我有一个在 Windows 10 上运行的 winui 3 桌面应用程序。在导航视图的设置页面中,我想允许用户更改环境并在主窗口的标题中指示环境。
在主窗口的代码中,我最初设置了标题:
namespace MetricReporting
{
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
this.Title = $"EMB Metric Reporting Tool {getSelectedEnvironment()}";
MasterContentFrame.Navigate(typeof(pageHome));
MasterNavigation.Header = "Home";
// Retrieve the window handle (HWND) of the current WinUI 3 window.
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
// For EPPlus spreadsheet library for .NET
ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial;
}
private string getSelectedEnvironment()
{
return " (Non-Production)";
}
在“设置”页面上的按钮单击方法中,我不知道如何引用主窗口的标题:
private void envProd_Click(object sender, RoutedEventArgs e)
{
DisplayEnvProddDialog();
MainWindow.Title = $"EMB Metric Reporting Tool (Production)";
}
上面的代码有语法错误 cs0120:非静态字段、方法或属性“Window.Title”需要对象引用
请帮忙。
谢谢你。
在 MainWindow 类中添加一个方法来更新标题
public void UpdateTitle(string environment)
{
this.Title = $"EMB Metric Reporting Tool {environment}";
}
然后在您的设置页面中添加对主窗口的引用:
private MainWindow mainWindow;
public SettingsPage(MainWindow mainWindow)
{
this.mainWindow = mainWindow;
this.InitializeComponent();
}
然后修改按钮单击事件处理程序:
private void envProd_Click(object sender, RoutedEventArgs e)
{
DisplayEnvProdDialog();
mainWindow.UpdateTitle(" (Production)");
}
然后您可以从主窗口导航到您的设置页面,传递此引用:
MasterContentFrame.Navigate(typeof(SettingsPage), this);