I am writing an app using navigation view
basing on Using the NavigationView in your UWP applications tutorial.
I hope someone can help to clarify 2 things
what is the best practice to place my general routines? in the
MainPage.xaml.cs
or thexaml
in respective views?How do i update my
xaml
element such astextblock
in a different view? eg. I have areadHardwareID
routine run at startup to read the hardware ID inMainPage.xaml.cs
How do I display the information inInfoPage.xaml
.
Please advise, Thanks.
Updated: 06-08-2020
I am trying to pass a simple AppVerison text
from MainPage to InfoPage to test out OnNavigateTo
. However when I run the code and clicked on the info
tab, I ran into this error.
MainPage Code
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;
}
}
}
InfoPage Code
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;
}
}