简体   繁体   English

WPF XAML Hello World从零开始,并有代表

[英]WPF XAML Hello World from scratch, with delegates

I want to get a XAML specified interface on the screen so I can learn WPF. 我想在屏幕上获取XAML指定的界面,以便可以学习WPF。 But I do not want to start from a visual studio project which has all the wire-up already done. 但是我不想从已经完成所有连线的Visual Studio项目开始。 I wan't the c# "hello world" equavalent of drawing a window specified in a xaml file on the screen from the "main()" function. 我不希望c#“ hello world”等同于在屏幕上通过“ main()”函数在xaml文件中指定一个窗口。

So how do I wire-up a XAML file to an object and draw it on the screen from a console app ? 那么,如何将XAML文件连接到对象并通过控制台应用程序将其绘制在屏幕上? Please wire-up a simple event as well. 请同时为一个简单的事件进行连线。 Say a "hello world" button (specified in xaml), that when pressed makes the console print "hello world". 说一个“ hello world”按钮(以xaml指定),按下该按钮可使控制台打印“ hello world”。 I'd prefer to see the delegate function in the following ways. 我希望通过以下方式查看委托函数。

  1. The logic as inline c# in XAML. XAML中的内联c#逻辑。
  2. The logic specified in the same file as the "main()" method, as a free function (edit : oh wait c# doesn't have free functions, well a delegate of some sort that isn't code behind). 在与“ main()”方法相同的文件中指定的逻辑,作为一个自由函数(编辑:哦,等待c#没有自由函数,还有某种形式的委托,它没有后面的代码)。
  3. The logic as a member function of an object, where I believe the object to be the codebehind of the XAML (assuming ofcourse this way of mapping between xaml and c# objects is possible). 作为对象的成员函数的逻辑,我认为该对象是XAML的代码背后(当然,可以假设在xaml和c#对象之间采用这种映射方式)。

Additionally : This answer shows the ui runloop taking over the main thread, is there any sugar to create a new thread for the app so it doesn't block the callee ? 另外: 该答案显示了ui runloop接管了主线程,是否有任何糖可以为该应用创建一个新线程,从而不会阻塞被调用者?

If your wondering why I am asking this question, or why I want to learn WPF this way. 如果您想知道为什么我要问这个问题,或者为什么我想以这种方式学习WPF。 I want to trigger ad-hoc UI elements from within a plugin that has no other means of displaying information. 我想从没有其他显示信息方式的插件中触发临时UI元素。


For those that find this question too vague, here is an example of xaml 对于那些觉得这个问题太模糊的人,这里是xaml的示例

<Canvas xmlns="http://schemas.microsoft.com/client/2007"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <TextBlock>Hello World!</TextBlock>
</Canvas>

The main app in this question is a console c# app, the XAML file represents a form with a single button, pressing the button prints "hello world" to the console. 这个问题中的主要应用是控制台c#应用,XAML文件表示具有单个按钮的表单,按下该按钮将向控制台输出“ hello world”。 And I want to see the different ways of wiring up the button to my code, I do not understand how I could be clearer. 而且我想了解将按钮连接到代码的不同方式,但我不知道如何更清晰。

At the risk of being told this is not what you asked, but since it's not clear: 冒被告知的危险不是您要的,但由于不清楚:

class WpfSample
{
    [STAThread]
    public static void Main()
    {
        var window = new Window()
        {
            Title = "WPF",
            Width = 640,
            Height = 480
        };

        var grid = new Grid();

        var button = new Button()
        {
            Content = "Click Me",
            Width = 100,
            Height = 50,

        };

        grid.Children.Add(button);
        window.Content = grid;

        button.Click += (s, e) =>
        {
            MessageBox.Show("You clicked me");
        };

        window.ShowDialog();

    }
}

That being said, if you want plugins for your WPF application, doing it like this (all with code) is not the way to go. 话虽这么说,但是如果您想要WPF应用程序的插件,那么这样做(全部使用代码)是不可行的。

We define controls en such in plugins without the need for hooking it all up in code, we use MEF for that. 我们在插件中定义的控制连接,例如无需钩住它的代码加起来,我们使用MEF了点。

Update To load the UI from some XAML you stored somewhere, you can use a ValueConverter 更新若要从存储在某处的某些XAML加载UI,可以使用ValueConverter

public class StringToXamlValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string xaml;
        if (value != null && !String.IsNullOrWhiteSpace(value.ToString))
        {
            xaml = value.ToString();
        }
        else
        {
            xaml = Settings.Default.DefaultLayoutView;
        }

        var root = XamlReader.Parse(xaml);
        return root;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

You'd then set the content of some element like this: 然后,您会设置一些这样的元素的内容:

<ContentControl Content="{Binding Layout.View, Converter={StaticResource StringToXamlConverter}}"/>

To cut straight to the point: 切入要点:

I want to trigger ad-hoc UI elements from within a plugin that has no other means of displaying information. 我想从没有其他显示信息方式的插件中触发临时UI元素。

There is absolutely no need to draw a window from scratch, instead you can use the ShowDialog() method on the Window object: 绝对没有任何需要画一个从无到有的窗口,而不是可以使用ShowDialog()方法的Window对象:

var myWindow = new Window();
myWindow.ShowDialog();

of course you can programmatically add whatever controls you want to that window, or you could have it already defined as XAML somewhere which you then re-hydrate as a window. 当然,你可以编程任何你想要的控件添加到该窗口,或者你可以有它已被定义为XAML的地方,你再重新水合物作为一个窗口。

Having said that, ideally the host of your plugin should provide you with a container that you can add content to (or provide a dialog service that you've hand rolled), rather then you having to forcibly show stuff directly from the plugin. 话虽如此,理想情况下,插件的宿主应该为您提供一个可以向其添加内容的容器(或提供您手动滚动的对话框服务),而不是必须直接从该插件强制显示内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM