无法创建MainView(UserControl)C#WPF的实例

时间:2019-06-18 16:35:21

标签: c# wpf

在尝试创建MainView实例(在我的GameLauncher.xaml都会窗口中作为UserControl的实例)时出现错误。

漏洞的相关代码:

public MainViewModel(IDialogCoordinator instance)
{
    Games = new ObservableCollection<Game>();
    Scanner = new GameScanner();
    Scanner.Scan();

    Games = Scanner.GetExecutables(); // If i comment out this line. thebug goes away
    FilteredGames = CollectionViewSource.GetDefaultView(Games);
}

方法本身:

public ObservableCollection<Game> GetExecutables()
{
    ObservableCollection<Game> games = new ObservableCollection<Game>();

    foreach (Platform libDir in LibraryDirectories)
    {
        string[] gameDirs = Directory.GetDirectories(libDir.InstallationPath);
        foreach (var gameDir in gameDirs)
        {
            string[] exes = Directory.GetFiles(gameDir, "*.exe", SearchOption.AllDirectories);
            Game game = new Game
            {
                Name = new DirectoryInfo(gameDir).Name,
                Platform = libDir.PlatformType,
                Executables = new List<string>(exes)
            };

            games.Add(game);
        }
    }
    return games;
}

正在实例化视图并引发错误:

<Controls:MetroWindow x:Class="GameLauncher.MainWindow"
                      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
                      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                      xmlns:views="clr-namespace:GameLauncher.Views"
                      xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
                      xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
                      Dialog:DialogParticipation.Register="{Binding}"
                      mc:Ignorable="d"
                      Title="Game Launcher"
                      Height="800" 
                      Width="1200" 
                      WindowStartupLocation="CenterScreen"
                      Background="#151518">
    <DockPanel>
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="File">
                <MenuItem Header="Exit" Command="{Binding ExitCommand}"/>
            </MenuItem>
            <MenuItem Header="Preferences">
                <MenuItem Header="Theme">
                    <MenuItem Header="Dark" Command="{Binding ChangeThemeCommand}" CommandParameter="Dark"/>
                    <MenuItem Header="Light" Command="{Binding ChangeThemeCommand}" CommandParameter="Light"/>
                </MenuItem>
                <MenuItem Header="Accent">
                    <MenuItem Header="Blue" Command="{Binding ChangeAccentCommand}" CommandParameter="Blue"/>
                    <MenuItem Header="Green" Command="{Binding ChangeAccentCommand}" CommandParameter="Green"/>
                    <MenuItem Header="Red" Command="{Binding ChangeAccentCommand}" CommandParameter="Red"/>
                    <MenuItem Header="Yellow" Command="{Binding ChangeAccentCommand}" CommandParameter="Yellow"/>
                    <MenuItem Header="Orange" Command="{Binding ChangeAccentCommand}" CommandParameter="Orange"/>
                    <MenuItem Header="Purple" Command="{Binding ChangeAccentCommand}" CommandParameter="Purple"/>
                    <MenuItem Header="Pink" Command="{Binding ChangeAccentCommand}" CommandParameter="Pink"/>
                    <MenuItem Header="Steel" Command="{Binding ChangeAccentCommand}" CommandParameter="Steel"/>
                    <MenuItem Header="Olive" Command="{Binding ChangeAccentCommand}" CommandParameter="Olive"/>
                    <MenuItem Header="Teal" Command="{Binding ChangeAccentCommand}" CommandParameter="Teal"/>
                    <MenuItem Header="Magenta" Command="{Binding ChangeAccentCommand}" CommandParameter="Magenta"/>
                    <MenuItem Header="Lime" Command="{Binding ChangeAccentCommand}" CommandParameter="Lime"/>
                    <MenuItem Header="Emerald" Command="{Binding ChangeAccentCommand}" CommandParameter="Emerald"/>
                </MenuItem>
            </MenuItem>
            <MenuItem Header="Help">
                <MenuItem Header="About" Command="{Binding ShowAboutCommand}"/>
            </MenuItem>
        </Menu>
        <views:MainView></views:MainView> //instanciate the view, this throws the error

    </DockPanel>
</Controls:MetroWindow>

实例化视图模型的地方:

namespace GameLauncher.Views
{
    public partial class MainView : UserControl
    {

        public MainView()
        {
            InitializeComponent();
            MainViewModel vm = new MainViewModel(DialogCoordinator.Instance);
            DataContext = vm;
        }
    }
}
尝试在我的Metro窗口中实例化usercontrol时出现

错误消息: 值不能为空。参数名称:路径

0 个答案:

没有答案