c# - 我如何处理自定义应用程序设置? Windows 10 UWP C#

标签 c# windows-10 uwp

我正在开发基于 webview 的应用程序。 Webview 是我的应用程序的核心,它位于主页上。我想让用户能够自定义应用程序。 例如: 我在主页中有 splitview。 splitview的默认显示模式是compactoverlay,如果用户设备是手机,它会自动更改为overlay。我想更改用户的这个可选择项。我为此做了一些事情,但效果不佳。谁能告诉我应该如何处理我的应用程序的设置?

public sealed partial class MainPage : Page
{
    public MainPage()
    {

        this.InitializeComponent();
        SettingsReader();
        ApplyUserSettings();
        NavigationCacheMode = NavigationCacheMode.Enabled;

        SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
        SystemNavigationManager.GetForCurrentView().BackRequested += (s, a) =>
        {


            if (webView.CanGoBack)
            {
                webView.GoBack();
                a.Handled = true;
            }

            SettingsReader();
            ApplyUserSettings();
        };
    }



    private async void SettingsReader()
    {
        try
        {
            await ReadFileSwitchSplitDisplayMode();
        }
        catch (Exception)
        {

        }

    }

    private void ApplyUserSettings()
    {
        if (tbxSwitchSplitDisplayMode.Text == "1")
        {

            ShellSplitView.DisplayMode = SplitViewDisplayMode.Overlay;
            mainpageAppBarGrid.Margin = new Thickness(48, 0, 0, 0);
        }
        else ShellSplitView.DisplayMode = SplitViewDisplayMode.CompactOverlay;
    }

设置页面

public sealed partial class Settings : Page
{

    public Settings()
    {
        InitializeComponent();
        //NavigationCacheMode = NavigationCacheMode.Enabled;
        SettingsReader();
        tglSwitchSplitDisplayModeCheck();

    }

    private async void SettingsReader()
    {
        try
        {
            await ReadFileSwitchSplitDisplayMode();
        }
        catch (Exception)
        {


        }

    }



    private async void btnKaydet_Click(object sender, RoutedEventArgs e)
    {
        await WriteToFile();
        await WriteToFileSwitchSplitDisplayMode();

        //Show Success Message
        var dlg = new MessageDialog("Kaydedilen ayarların tamamının uygulanması için uygulamanın sizin tarafınızdan yeniden başlatılması gerekiyor. Şimdi kapatılsın mı ?","Ayarlar Kaydedildi!");
        dlg.Commands.Add(new UICommand("Evet", null, "YES"));
        dlg.Commands.Add(new UICommand("Hayır", null, "NO"));
        var op = await dlg.ShowAsync();
        if ((string)op.Id == "YES")
        {
            App.Current.Exit();
        }
    }






        }

    }



    private void tglSwitchSplitDisplayModeCheck()
    {
        if (tbxSwitchSplitDisplayMode.Text == "1")
        {
            tglSwitchSplitDisplayMode.IsOn = true;
        }
        else
            tglSwitchSplitDisplayMode.IsOn = false;
    }

    public async Task WriteToFileSwitchSplitDisplayMode()
    {
        // Get the text data from the textbox
        byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(tbxSwitchSplitDisplayMode.Text.ToCharArray());

        //Get the local folder
        StorageFolder local = ApplicationData.Current.LocalFolder;

        //Create new folder name DataFolder
        var dataFolder = await local.CreateFolderAsync("Data Folder", CreationCollisionOption.OpenIfExists);

        //Create txt file
        var file = await dataFolder.CreateFileAsync("tglSwitchSplitDisplayMode.txt", CreationCollisionOption.ReplaceExisting);

        //write the data from the text box
        using (var s = await file.OpenStreamForWriteAsync())
        {
            s.Write(fileBytes, 0, fileBytes.Length);
        }

    }

    public async Task ReadFileSwitchSplitDisplayMode()
    {
        // Get the local folder.
        StorageFolder local = ApplicationData.Current.LocalFolder;

        if (local != null)
        {
            // Get the DataFolder folder.
            var dataFolder = await local.GetFolderAsync("Data Folder");

            // Get the file.
            var file = await dataFolder.OpenStreamForReadAsync("tglSwitchSplitDisplayMode.txt");

            // Read the data.
            using (StreamReader streamReader = new StreamReader(file))
            {
                tbxSwitchSplitDisplayMode.Text = streamReader.ReadToEnd();
            }

        }
    }

    private void tglSwitchSplitDisplayMode_Toggled(object sender, RoutedEventArgs e)
    {
        if (tglSwitchSplitDisplayMode.IsOn)
        {
            tbxSwitchSplitDisplayMode.Text = "1";
        }
        else tbxSwitchSplitDisplayMode.Text = "0";
    }
}

enter image description here 因此,它现在仅在用户退出应用程序并重新启动时才起作用。然而这个技巧不适用于 Windows Phone 10。

最佳答案

针对您的场景,有不同的解决方案:

1) 如果您想根据某些系统设置修改 UI,我同意 ApplicationData.LocalSetting API 很好。您可以找到说明here .

2)根据您的描述,似乎使 UI 适应不同设备更方便。在这种情况下,XAML 的自适应 UI 功能可能更适合您。 Here您可以找到有关自适应 UI 开发的教程; Here您可以找到示例。

请告诉我这是否有帮助。

关于c# - 我如何处理自定义应用程序设置? Windows 10 UWP C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33199792/

相关文章:

c# - 在 MVC 中将值从 Controller 传递到 View

c# Process.GetCurrentProcess().Threads.Count 在使用 MaxDegreeOfParallelism 时未返回预期?

c++ - delphi - 调用外部WinAPI函数

xaml - 我可以使用视觉状态更改对象的 Canvas.Zindex 吗?

c# - 在没有 ms-app 的情况下从 OAuth 重定向到 UWP 应用程序

c# - AesCryptoServiceProvider,性能问题

c# - 如何确定我的安装程序是否在 Windows 10 教育版上运行?

powershell - 从命令行在 Windows 10 中获取文件类型关联的最佳方法?

c# - UWP App 中的垂直多级导航菜单

c# - 如何为 C# 项目生成 .tlh 文件