简体   繁体   中英

How can i handle custom app settings ? Windows 10 UWP c#

I'm working on webview based app. Webview is core of my app and it's on Mainpage. I want to give user to be able to customize the app. For example: I have splitview in Mainpage. Default display mode of splitview is compactoverlay and if user device is phone it changes automatically to overlay. I want to change this selectable for user. I did something for that but it's not working efficiently. Could anyone tell me how should i handle settings of my app ?

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;
    }

Settings Page

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

在此处输入图片说明 As a result, it's now working only when user exit from app and launch back. However this trick not work for windows phone 10.

For your scenario, there have different solutions:

1) If you want to modify the UI based on some system settings, I agree that the ApplicationData.LocalSetting APIs is good. You can find instructions here .

2) Per your descriptions, it seems that it is more convenient to make the UI adaptive for different devices. In that case, the XAML's feature about adaptive UI may be more suitable for you. Here you can find a tutorial about adaptive UI development; Here you can find samples.

Please let me know if this can help.

I did it.

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

        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM