UWP - 从 TabView 类保存选项卡 [英] UWP - Saving Tabs From The TabView class

查看:48
本文介绍了UWP - 从 TabView 类保存选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 UWP 应用程序关闭时保存最终用户创建的数据(选项卡)以重新打开选项卡.

how do I save the data (tabs) created by the end user to reopen the tabs when the UWP application is closed.

using Windows.UI.Xaml.Controls;

namespace Network
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();


        }

        private void NavigationView_OnSelectionChanged(NavigationView sender,
            NavigationViewSelectionChangedEventArgs args)
        {
            if (args.SelectedItemContainer.Tag.Equals("Users"))
            {
                Frame.Navigate(typeof(Interface));

            }

            if (args.SelectedItemContainer.Tag.Equals("Interception"))
            {
                Frame.Navigate(typeof(Network));
            }
        }


    }
}

推荐答案

如何在 UWP 应用程序关闭时保存最终用户创建的数据(选项卡)以重新打开选项卡.

how do I save the data (tabs) created by the end user to reopen the tabs when the UWP application is closed.

当然,您可以将 Tabs 数据保存为 json 字符串,然后在应用程序关闭期间存储在本地文件夹中.当您重新打开应用程序时,您可以将 json 字符串反序列化为选项卡的数据.

Sure, you could save the Tabs data as json string, then store in the local folder during the app closing. When you reopen the app, you could deserialize the json string to the tab's data.

例如

public MainPage()
{
    this.InitializeComponent();

    App.Current.Suspending += Current_Suspending;
}
private ObservableCollection<TabData> Tabs;
private void Current_Suspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e)
{
    if (Tabs.Count > 0)
    {
        var json = JsonConvert.SerializeObject(Tabs);
        ApplicationData.Current.LocalSettings.Values["TabsData"] = json;
    }

}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    var json = ApplicationData.Current.LocalSettings.Values["TabsData"] as string;
    if (json != null)
    {
        Tabs = JsonConvert.DeserializeObject<ObservableCollection<TabData>>(json);

    }
    else
    {
        Tabs = new ObservableCollection<TabData>();
        Tabs.Add(new TabData( "page1",  Type.GetType( "App19.TestPage")));          
    }
}


private void TabView_Loaded(object sender, RoutedEventArgs e)
{
    (sender as TabView).TabItemsSource = Tabs;

}

private void TabView_AddButtonClick(TabView sender, object args)
{
    Tabs.Add(new TabData("newPage", typeof(TestPage)));
}

private void TabView_TabCloseRequested(TabView sender, TabViewTabCloseRequestedEventArgs args)
{
    Tabs.Remove(args.Item as TabData);

}

标签类

public class TabData
{
    public TabData(string header, Type frameType)
    {
        Header = header;
        FrameType = frameType;
    }
    public string Header { get; set; }
    public Type FrameType { get; set; }
}

Xaml

<muxc:TabView
    AddTabButtonClick="TabView_AddButtonClick"
    Loaded="TabView_Loaded"
    TabCloseRequested="TabView_TabCloseRequested">
    <muxc:TabView.TabItemTemplate>
        <DataTemplate>
            <muxc:TabViewItem Header="{Binding Header}" >
                <muxc:TabViewItem.IconSource>
                    <muxc:SymbolIconSource Symbol="Document"/>
                </muxc:TabViewItem.IconSource>
                <Frame SourcePageType="{Binding FrameType}" />
            </muxc:TabViewItem>
        </DataTemplate>
    </muxc:TabView.TabItemTemplate>
</muxc:TabView>

这篇关于UWP - 从 TabView 类保存选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆