WPFウィンドウを定義して開こうとするroslynスクリプトを実行しています。とりわけWPF/XAML:任意の名前空間内で定義されていないクラスを参照する方法
、私のスクリプト
- が添付行動
- を定義するには、私はWPFウィンドウを作成するかに基づいてXAMLの文字列を定義します。このXAMLコードでは、スクリプトで定義されたTextBoxCursorPositionBehaviorを使用したいと考えています。
私のスクリプトは、(.csx)ファイルが
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が
<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>
のように見えます。しかし、問題は、私が正しくTextBoxCursorPositionBehavior参照する方法、であると仮定XAML?
Roslynはスクリプトファイル内で名前空間を使用できないため、TextBoxCursorPositionBehaviorを名前空間の外に定義する必要があります(つまり、グローバル名前空間に入ると仮定します)。
しかし、それをXAMLでどのように参照できますか?私は名前空間の参照を "clr-namespace:; assembly =" + typeof(TextBoxCursorPositionBehavior).ToString()で定義しようとしましたが、それは動作しません。 単に "clr-namespace:"(つまりアセンブリ参照なし)も機能しません。
XAML定義内からTextBoxCursorPositionBehaviorを参照する方法はありますか?あなたのコードの代わりに使用するアセンブリで
CSharpScript.Evaluateを使用して、スクリプトをどれだけ正確に実行していますか? – Evk
私はSourceCodeKind = Scriptを使用してメモリ内アセンブリをコンパイルしています。サブミッションタイプを調べるか、ファクトリメソッドを自分で呼び出します。 これは、CSharpScript.Create()と.RunAsync()と比較してうまくいくはずです。特に、アセンブリの作成方法、サブミッションクラスの設定方法などはすべてRoslynのデフォルトですが、私はそこに干渉していません。 – Bogey