2015-10-18 55 views
2

我正在研究基于webview的应用程序。 Webview是我的应用程序的核心,它在Mainpage上。我想让用户能够自定义应用程序。例如: 我在主页上有splitview。 splitview的默认显示模式是compactoverlay,如果用户设备是手机,它会自动更改为覆盖。我想为用户更改此可选项。我为此做了一些事情,但效率不高。任何人都可以告诉我应该如何处理我的应用程序的设置?我如何处理自定义应用程序设置? Windows 10 UWP c#

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

您是否尝试过使用ApplicationData.LocalSettings API?我认为这是更好的方式来实现这样的基本设置。 – Abdousamad

回答

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

     } 

    } 
2

工作的情况下,有不同的解决方案:

1)如果要基于一些系统设置修改的UI,我同意ApplicationData.LocalSetting的API很好。你可以找到说明here

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

请让我知道,如果这可以帮助。

+0

ApplicationData.LocalSetting运行良好。我解决了这个问题并学习了如何存储设置数据。谢谢。我认为这个页面更有帮助。 https://msdn.microsoft.com/en-us/library/windows/desktop/mt299098.aspx – ArdaZeytin

相关问题