如何处理自定义应用程序设置?windows10uwpc#

如何处理自定义应用程序设置?windows10uwpc#,c#,windows-10,uwp,C#,Windows 10,Uwp,我正在开发基于webview的应用程序。Webview是我的应用程序的核心,它位于主页上。我想让用户能够自定义应用程序。 例如: 我在主页上有splitview。splitview的默认显示模式为compactoverlay,如果用户设备为phone,则会自动更改为overlay。我想为用户更改此选项。我为此做了点什么,但效果不佳。有人能告诉我应该如何处理我的应用程序的设置吗 public sealed partial class MainPage : Page { public Mai

我正在开发基于webview的应用程序。Webview是我的应用程序的核心,它位于主页上。我想让用户能够自定义应用程序。 例如: 我在主页上有splitview。splitview的默认显示模式为compactoverlay,如果用户设备为phone,则会自动更改为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";
    }
}

所以,现在只有当用户退出应用程序并重新启动时,它才起作用。然而,这个技巧不适用于WindowsPhone10

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

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

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

请告诉我这是否有帮助。

我做到了

设置页面

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");


    }
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)
        {

        }

    }
主页

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");


    }
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)
        {

        }

    }

您是否尝试过使用ApplicationData.LocalSettings API?我认为这是实现这样的基本设置的更好方法。ApplicationData.LocalSetting运行良好。我解决了这个问题,并学习了如何存储设置数据。谢谢我认为这一页更有帮助。