我正在使用 UWP 应用程序教程中navigation view
的使用 NavigationView 编写应用程序。我希望有人可以帮助澄清两件事
放置我的一般例程的最佳做法是什么?在各自的观点中
MainPage.xaml.cs
还是xaml
在各自的观点中?如何更新我的
xaml
元素,例如textblock
在不同的视图中?例如。我readHardwareID
在启动时运行了一个例程来读取硬件 IDMainPage.xaml.cs
如何在InfoPage.xaml
.
请指教,谢谢。
更新时间:2020 年 6 月 8 日
我正在尝试将一个简单的 AppVerisontext
从 MainPage 传递到 InfoPage 以进行测试OnNavigateTo
。但是,当我运行代码并单击info
选项卡时,我遇到了这个错误。
主页代码
public String AppVersionName = "Test version 1.0";
private void nvTopLevelNav_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
{
if (args.IsSettingsInvoked)
{
contentFrame.Navigate(typeof(SettingsPage));
} else
{
TextBlock ItemContent = args.InvokedItem as TextBlock;
if (ItemContent != null)
{
switch (ItemContent.Tag)
{
case "Nav_Home":
contentFrame.Navigate(typeof(HomePage));
break;
case "Nav_Devices":
contentFrame.Navigate(typeof(DevicesPage));
break;
case "Nav_Log":
contentFrame.Navigate(typeof(LogPage));
break;
case "Nav_Info":
contentFrame.Navigate(typeof(InfoPage));
break;
}
}
}
信息页代码
public sealed partial class InfoPage : Page
{
private MainPage mainPage;
string appVersion;
public InfoPage(MainPage page)
{
this.InitializeComponent();
mainPage = page;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
var data = e.Parameter;
appVersion = mainPage.AppVersionName;
readHardwareID();
}
public void readHardwareID()
{
var deviceIdtoken = HardwareIdentification.GetPackageSpecificToken(null);
var deviceId = deviceIdtoken.Id;
var deviceIdReader = DataReader.FromBuffer(deviceId);
byte[] deviceIdbytes = new byte[deviceId.Length];
deviceIdReader.ReadBytes(deviceIdbytes);
DeviceID.Text = BitConverter.ToString(deviceIdbytes);
var sysIdToken = SystemIdentification.GetSystemIdForPublisher();
var sysId = sysIdToken.Id;
var sysIdReader = DataReader.FromBuffer(sysId);
byte[] sysIdbytes = new byte[sysId.Length];
sysIdReader.ReadBytes(sysIdbytes);
SystemID.Text = BitConverter.ToString(sysIdbytes);
// get the system family information
string deviceFamily = AnalyticsInfo.VersionInfo.DeviceFamily;
Device.Text = deviceFamily;
// get the system version number
var deviceFamilyVersion = AnalyticsInfo.VersionInfo.DeviceFamilyVersion.ToString();
var version = ulong.Parse(deviceFamilyVersion);
var majorVersion = (version & 0xFFFF000000000000L) >> 48;
var minorVersion = (version & 0x0000FFFF00000000L) >> 32;
var buildVersion = (version & 0x00000000FFFF0000L) >> 16;
var revisionVersion = (version & 0x000000000000FFFFL);
var systemVersion = $"{majorVersion}.{minorVersion}.{buildVersion}.{revisionVersion}";
OSVersion.Text = systemVersion.ToString();
AppVersion.Text = appVersion;
}
}