App.xaml 中 MainWindow.xaml.cs 的 Click-Event [英] Click-Event from MainWindow.xaml.cs in App.xaml

查看:37
本文介绍了App.xaml 中 MainWindow.xaml.cs 的 Click-Event的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想将一些东西从 MainWindow.xaml 外包到 App.xaml,例如:

so i want to outsource some things from MainWindow.xaml to App.xaml like this for example :

<Application x:Class="SVGTesting.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <DataTemplate DataType="{x:Type ContentControl}" x:Key="Test1">
                <StackPanel Orientation="Horizontal">
                    <Button Content="Button1" Click="Button_Click" x:Name="Button1"/>
                    <Button Content="Button2" Click="Button_Click" x:Name="Button2"/>
                </StackPanel>
            </DataTemplate>
        </ResourceDictionary>
    </Application.Resources>
</Application>

在 MainWindow.xaml 然后我有这样的东西

In MainWindow.xaml then i have something like this

<ContentControl ContentTemplate="{StaticResource Test1}"/>

但现在 VS 说我不能使用函数Button_Click",因为它不在 App.xaml 的代码隐藏中.那么如何从 App.xaml 的 MainWindow 调用这个函数呢?

But now VS says that i cannot use the function "Button_Click" because its not in the codebehind from App.xaml. So how can i call this function from MainWindow in App.xaml?

有什么办法吗?我不想要像 MVVM 或 Command 这样的答案.如果无法解决,那么不幸的是,WPF 对我来说毫无用处.

Is there any way? I don't want answers like MVVM or Command. If it's not possible to solve then WPF is unfortunately useless for me.

谢谢和问候.

推荐答案

这不是最简单的事情,因为 WPF 期望事情以自己的方式完成.但是从最简单到最难的选择很少.

This is not the easiest thing to do as WPF expect things to be done in its own way. But there's few options, from easiest to hardest.

1.什么都不做

最简单的方法是将数据模板保存在 MainWindow.xaml 中.

Easiest way is to keep your data templates inside the MainWindow.xaml.

2.使用命令代替事件处理程序

您当前定义的事件处理程序如下:

You currently have event handlers defined like this:

<Button Content="Button1" Click="Button_Click"

这样做的更多 WPF 方式"是用具有这种非常繁琐的语法的命令替换 Click 的事件处理程序:

"More-WPF way" of doing this would be to replace Click's event handler with a command with this quite cumbersome syntax:

        <Button Content="Test" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.OnClickCommand}"></Button>

然后在你的主窗口中定义命令:

And then define the command in your MainWindow:

    public ICommand OnButtonClick
    {
        get
        {
            return new Command(() =>
            {
                this.Text.Text = "Updated";
            });
        }
    }

3.在 App.xaml.cs 中定义事件处理程序并使用它来路由事件处理程序

我不建议这样做,因为保持同步会让人厌烦,但这是可能的.在 App.xaml.cs 中创建和事件处理程序:

I don't recommend this as it get tiresome to keep things synced but it's possible. Create and event handler in App.xaml.cs:

public partial class App : Application
{
    private void Button_Click(object sender, RoutedEventArgs e)
    {

    }
}

然后使用发送方访问 MainWindow 实例并调用它的方法:

Then use the sender to access the MainWindow instance and call it's method:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var mainWindow = (MainWindow)Window.GetWindow((DependencyObject)sender);

        mainWindow.DoWork();
    }

在我的第二个示例中,Command 的定义如下:

In my second example Command is defined like the following:

    public class Command : ICommand
    {
        public delegate void ICommandOnExecute();
        private ICommandOnExecute _execute;

        public event EventHandler CanExecuteChanged;

        public Command(ICommandOnExecute onExecuteMethod)
        {
            _execute = onExecuteMethod;
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            _execute?.Invoke();
        }
    }

这篇关于App.xaml 中 MainWindow.xaml.cs 的 Click-Event的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆