WPF/XAML:如何引用未在任何命名空间中定义的类 [英] WPF/XAML: How to reference class that is not defined within any namespace

查看:51
本文介绍了WPF/XAML:如何引用未在任何命名空间中定义的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行一个尝试定义和打开 WPF 窗口的 roslyn 脚本.

I'm executing a roslyn script that tries to define and open a WPF window.

除其他外,我的脚本

  1. 定义附加行为
  2. 定义了一个 XAML 字符串,我基于它创建了一个 WPF 窗口.在此 XAML 代码中,我想使用脚本中定义的 TextBoxCursorPositionBehavior.

我的脚本 (.csx) 文件看起来类似于

my script (.csx) file looks similar to

public class TextBoxCursorPositionBehavior : DependencyObject
{
    // see http://stackoverflow.com/questions/28233878/how-to-bind-to-caretindex-aka-curser-position-of-an-textbox
}

public class MyGui
{
    public void Show()
    {
      string xaml = File.ReadAllText(@"GUI_Definition.xaml");

      using (var sr = ToStream(xaml))
      {
        System.Windows.Markup.ParserContext parserContext = new System.Windows.Markup.ParserContext();
        parserContext.XmlnsDictionary.Add( "", "http://schemas.microsoft.com/winfx/2006/xaml/presentation" );
        parserContext.XmlnsDictionary.Add( "x", "http://schemas.microsoft.com/winfx/2006/xaml" );
        parserContext.XmlnsDictionary.Add("i","clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity");

        // ?? How  can i define this properly?
        parserContext.XmlnsDictionary.Add("behaviors", "clr-namespace:;assembly=" + typeof(TextBoxCursorPositionBehavior).Assembly.FullName);

        var window = (System.Windows.Window)XamlReader.Load(sr, parserContext);
        window.ShowDialog();
      }
    }
}

并假设 GUI_Definition.xaml 看起来像

and assume the GUI_Definition.xaml looks like

<Window x:Class="System.Windows.Window" Height="300" Width="300" >
<Grid>
  <!-- how can i attach my behavior here properly? -->
  <TextBox behaviors:TextBoxCursorPositionBehavior.TrackCaretIndex="True"/>
</Grid>
</Window>

但问题是,如何在 XAML 中正确引用 TextBoxCursorPositionBehavior?

But the problem is, how can I reference TextBoxCursorPositionBehavior correctly in XAML?

Roslyn 不允许在脚本文件中使用命名空间,因此必须在命名空间之外定义 TextBoxCursorPositionBehavior(即我认为它会落入全局命名空间).

Roslyn doesn't allow to use namespaces in script files, so TextBoxCursorPositionBehavior must be defined outstide of a namespace (i.e. I suppose it will fall into the global namespace).

但是,我如何在 XAML 中引用它?我尝试使用clr-namespace:;assembly="+ typeof(TextBoxCursorPositionBehavior).ToString() 定义命名空间引用,但这不起作用.简单的clr-namespace:"(即没有程序集引用)也不起作用.

But then, how can I reference it in XAML? I've tried defining the namespace reference with "clr-namespace:;assembly=" + typeof(TextBoxCursorPositionBehavior).ToString(), but that doesn't work. Simply "clr-namespace:" (i.e. without assembly reference) doesn't work either.

有没有办法从 XAML 定义中引用 TextBoxCursorPositionBehavior?

Is there any way to reference TextBoxCursorPositionBehavior from within the XAML definition?

推荐答案

我想我知道发生了什么...... Roslyn 为脚本创建了一个自定义提交类型,并且似乎所有东西——包括 TextBoxCursorPointerBehavior 的定义——都是一个子类这种提交类型.即,

I think I know what's happening ... Roslyn creates a custom Submission type for scripts, and seems everything - including the definition of TextBoxCursorPointerBehavior - is a sub-class of this submission type. I.e.,

        var inst = new TextBoxCursorPositionBehavior();
        string typeName = inst.GetType().FullName;

typeName 不会是TextBoxCursorPointerBehavior",而是Submission#0+TextBoxCursorPositionBehavior".

typeName will not be "TextBoxCursorPointerBehavior", but rather "Submission#0+TextBoxCursorPositionBehavior".

同时,我不能从 XAML 引用它(例如通过行为:Submission#0+TextBoxCursorPositionBehavior.TrackCaretIndex="True"),因为它不会正确解析名称(# 是那里的无效标记).

At the same time, I can NOT reference this from XAML (e.g. by behaviors:Submission#0+TextBoxCursorPositionBehavior.TrackCaretIndex="True") as it won't parse the name correctly (# is an invalid token there).

理论上,可以将 Roslyn 的提交类型重命名为可通过 XAML 实际引用的类型 - 但在我的情况下,我不能这样做.

In theory, it might be possible to rename Roslyn's submission type to something that is actually referencable via XAML - in my case though, I cannot do that.

不幸的是,目前这意味着我没有看到任何解决我的问题的方法,除了可能将此代码外包给单独的预编译 DLL(但这也不是编写脚本的重点)

Which unfortunately currently means I don't see any solution to my issue, other than possibly outsourcing this code to a separate pre-compiled DLL (but that's not quite the point of scripting either)

这篇关于WPF/XAML:如何引用未在任何命名空间中定义的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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