我该如何处理自定义应用设置? Windows 10 UWP c#

时间:2015-10-18 15:34:25

标签: c# windows-10 uwp

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

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不起作用。

2 个答案:

答案 0 :(得分:2)

对于您的方案,有不同的解决方案:

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

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

请告诉我这是否有帮助。

答案 1 :(得分:1)

我做到了。

<强> SettingsPage

private async void tglSwitchSplitDisplayMode_Toggled(object sender, RoutedEventArgs e)
    {

        StorageFolder local = ApplicationData.Current.LocalFolder;
        var dataFolder = await local.CreateFolderAsync("Data Folder", CreationCollisionOption.OpenIfExists);
        var file = await dataFolder.CreateFileAsync("SwitchSplitDisplayMode.txt", CreationCollisionOption.ReplaceExisting);

        if (tglSwitchSplitDisplayMode.IsOn)
        { 
            await FileIO.WriteTextAsync(file,"on");
        }
        else await FileIO.WriteTextAsync(file, "off");


    }

<强>的MainPage

private async void ApplyUserSettings()
    {
        try
        {
            StorageFolder local = ApplicationData.Current.LocalFolder;

            var dataFolder = await local.GetFolderAsync("Data Folder");
            var file = await dataFolder.GetFileAsync("SwitchSplitDisplayMode.txt");
            String SwitchSplitDisplayMode = await FileIO.ReadTextAsync(file);

            if (SwitchSplitDisplayMode == "on")
            {
                ShellSplitView.DisplayMode = SplitViewDisplayMode.Overlay;
                mainpageAppBarGrid.Margin = new Thickness(48, 0, 0, 0);
            }
            else ShellSplitView.DisplayMode = SplitViewDisplayMode.CompactOverlay;
        }
        catch (Exception)
        {

        }

    }