我可以使用XamlReader.Load或InitializeFromXaml从WPF窗口,为窗口定义? [英] Can I use XamlReader.Load or InitializeFromXaml from a WPF Window, for the Window definition?

查看:281
本文介绍了我可以使用XamlReader.Load或InitializeFromXaml从WPF窗口,为窗口定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生产将被纳入WPF应用程序的一些库code。库可能会弹出一个窗口,视情况而定。我可以定义在XAML窗口,但我想对待XAML作为模板。在运行时,在所述窗口正在被创建,以使其可显示的时间,我想替换某些标记在XAML模板运行时定义的值。

I want to produce some library code that will be included into WPF applications. The library may pop up a Window, depending on circumstances. I can define the Window in XAML, but I'd like to treat the XAML as a template. At runtime, at the time the Window is being created so that it can be displayed, I want to replace certain tags in the Xaml template with runtime-defined values.

我想要做的是这样的:

public partial class DynamicXamlWindow : Window
{
    Button btnUpdate = null;
    public DynamicXamlWindow()
    {
        string s = XamlTemplate;

        // replace some things in the XamlTemplate here

        Window root = System.Windows.Markup.XamlReader.Load(...);
        Object _root = this.InitializeFromXaml(new StringReader(s).ReadToEnd()); //??

        btnUpdate = // ???

        //InitializeComponent();
    }

在XamlTemplate字符串看起来是这样的:

The XamlTemplate string looks like this:

    private string XamlTemplate = @"
    <Window x:Class='@@CLASS'
            xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
            xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
            Title='@@TITLE' 
            Height='346' Width='380'>

        <Grid>
          ...

我在哪里见过一个按钮或部分被定义在XAML和动态加载的例子。但是,这不是一个按钮或部分。该XamlTemplate提供了XAML的实际窗口。

I've seen examples where a button or a section is defined in XAML and loaded dynamically. But this is not a button or section. The XamlTemplate provides the XAML for the actual Window.

这是可能的无论是与InitializeFromXaml或的XamlReader .Load 的?如果是这样,怎么样?

Is this possible either with InitializeFromXaml or XamlReader.Load ? If so, how?

我能否再找回在XAML中定义的控件,例如为btnUpdate在上面的code片段。怎么样?

Can I then retrieve the controls defined in the XAML, for example btnUpdate in the code fragment above. How?

推荐答案

您不能创建一个动态的页面,有在x:类属性。但是,如果code背后是相同的每个动态页面,您可以通过更改模板来诱骗:

You cannot create a dynamic page that has the x:class attribute. However if the code behind is the same for every dynamic page you can trick it by changing your template to:

private string XamlTemplate = @"
    <control:BaseWindow
            xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
            xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
            xmlns:control='WhateverHere'
            Title='@@TITLE' 
            Height='346' Width='380'>
        <Grid>...

当你准备好来分析这一点:

When you are ready to parse this use:

XamlReader.Parse(xaml);

如果你想访问在code项目的背后,你会this.FindName(为btnUpdate)在code-落后。

If you wanted to access items in the code behind you would this.FindName("btnUpdate") in the code-behind.

这篇关于我可以使用XamlReader.Load或InitializeFromXaml从WPF窗口,为窗口定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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