1

我有一个使用 Mvvm Light 和 Ninject 的 wpf 应用程序。在我的本地机器上,RelayCommand 工作正常,当我将应用程序移动到另一台服务器时,它不再调用该方法(不会弹出消息框)。

在 MainViewModel.cs 我有

public ICommand GoClickCommand
{
    get
    {
        return new RelayCommand(() => MessageBox.Show("Directly in property!"), () => true);
    }
}

在 MainWindow.xaml 我有

        `<Button Content="Go" HorizontalAlignment="Left" Margin="508,54,0,0" VerticalAlignment="Top" Width="75"
                Command="{Binding GoClickCommand}" IsDefault="True" Click="btn_go_Click"/>`

我在调用 btn_go_click 后面的代码中有一个方法,该方法在单击按钮时被调用,这只是为了确保按钮实际工作。我不确定它是否与 Ninject 有关。我正在尝试使应用程序自包含(只有一个 .exe 文件),因此我在 App.xaml.cs 文件中有代码来解决缺少的程序集。我尝试在没有此代码的情况下运行该应用程序,该应用程序需要的唯一 2 个程序集是 GalaSoft.MvvmLight.WPF.dll 和 Ninject.dll。我注释掉了代码并将这两个程序集放在与应用程序相同的文件夹中,并且 RelayCommand 仍然没有触发。实际应用程序中的代码更复杂,我更改了 GoClickCommand 属性中的代码以尝试找出问题所在。感谢您的任何帮助或建议。

//from http://www.paulrohde.com/merging-a-wpf-application-into-a-single-exe/
        {
            AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly;
            App.Main();
        }

        private static Assembly OnResolveAssembly(object sender, ResolveEventArgs e)
        {
            var thisAssembly = Assembly.GetExecutingAssembly();

            //Get the name of the AssemblyFile
            var assemblyName = new AssemblyName(e.Name);
            var dllName = assemblyName.Name + ".dll";

            //Load from Embedded Resources - This function is not called if the Assembly is already
            //in the same folder as the app.
            var resources = thisAssembly.GetManifestResourceNames().Where(s => s.EndsWith(dllName));
            var resourcesEnumerated = resources as string[] ?? resources.ToArray();
            if (resourcesEnumerated.Any())
            {
                //99% of cases will only have one matching item, but if you don't,
                //you will have to change the logic to handle those cases.
                var resourceName = resourcesEnumerated.First();
                using (var stream = thisAssembly.GetManifestResourceStream(resourceName))
                {
                    if (stream == null) return null;
                    var block = new byte[stream.Length];

                    //Safely try to load the assembly
                    try
                    {
                        stream.Read(block, 0, block.Length);
                        return Assembly.Load(block);
                    }
                    catch (IOException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    catch (BadImageFormatException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
            //in the case where the resource doesn't exist, return null.
            return null;
        }
    }
4

0 回答 0