2015-10-18 61 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

相關問題