我在MS VS 2015 Professional(.NET Framework 4.6)中开发WPF MVVM应用程序.当我运行我的WPF MVVM应用程序时,我的应用程序主窗口的两个实例正在显示.它为什么有位置?下面是我的应用程序主窗口的标记 - MainWindow.xaml:
<Window x:Class="SuperMrgaChartDrawer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SuperMrgaChartDrawer"
xmlns:vm="clr-namespace:SuperMrgaChartDrawer.ViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
下面是MainWindow.xaml.cs的代码:
using System.Windows;
namespace SuperMrgaChartDrawer
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
Run Code Online (Sandbox Code Playgroud)
以下是App.xaml.cs的代码:
using System;
using System.Windows;
using SuperMrgaChartDrawer.ViewModel;
namespace SuperMrgaChartDrawer
{
public partial class App : Application
{
/// <summary>
/// My application OnStartup handler.
/// </summary>
/// <param name="e"></param>
protected override void OnStartup(StartupEventArgs e)
{
// Call OnStartup in base class.
base.OnStartup(e);
// Create an instance of application main window.
MainWindow window = new MainWindow();
// Create an instance of View Model of main window.
var viewModel = new MainWindowViewModel();
// Define handler for "Main Window Request Close" event
// and subscribe on it.
EventHandler handler = null;
handler = delegate
{
viewModel.RequestClose -= handler;
window.Close();
};
viewModel.RequestClose += handler;
// Set MainWindowViewModel as main window data context.
window.DataContext = viewModel;
// Display main window.
window.Show();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这个错误的原因是什么?我该怎么做才能消除这个错误.请帮忙.我们将非常感谢您的帮助.
Den*_*nis 10
你的App.xaml包含StartupUri="MainWindow.xaml".此属性由WPF应用程序模板添加.StartupUri如果要手动显示窗口,请删除.
此外,这是添加事件处理程序的相当奇特的方式:
EventHandler handler = null;
handler = delegate
{
viewModel.RequestClose -= handler;
window.Close();
};
viewModel.RequestClose += handler;
Run Code Online (Sandbox Code Playgroud)
因为RequestClose就是EventHandler,你可以从上面的代码替换(这是主窗口,因此,没有必要退订):
viewModel.RequestClose += (sender, args) => window.Close();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
362 次 |
| 最近记录: |