XAML Support
Hi,
I would like to use LINQPad for WPF-Behavior testing with XAML. The code below results in an error. I think it's a problem with namespace specification of the behavior. Does anyone know how to specify the namespace correctly?
Regards
Oskar
:The CODE formatter doesn't show the code correctly. Some ";" are not shown correctly below!
I would like to use LINQPad for WPF-Behavior testing with XAML. The code below results in an error. I think it's a problem with namespace specification of the behavior. Does anyone know how to specify the namespace correctly?
Regards
Oskar
:The CODE formatter doesn't show the code correctly. Some ";" are not shown correctly below!
void Main()
{
var context = new ParserContext();
context.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
context.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
context.XmlnsDictionary.Add("beh", "clr-namespace:LINQPad.UserQuery");
var xaml = "<TextBox Text=\"{Binding Name, Mode = OneWay}\" beh:DigitsOnlyBehavior.IsDigitOnly=\"True\" ></TextBox>";
var element = (FrameworkElement)XamlReader.Parse(xaml, context);
element.DataContext = new { Name = "Test" };
PanelManager.StackWpfElement(element);
}
// Define other methods and classes here
public static class DigitsOnlyBehavior
{
public static bool GetIsDigitOnly(DependencyObject obj)
{
return (bool)obj.GetValue(IsDigitOnlyProperty);
}
public static void SetIsDigitOnly(DependencyObject obj, bool value)
{
obj.SetValue(IsDigitOnlyProperty, value);
}
public static readonly DependencyProperty IsDigitOnlyProperty =
DependencyProperty.RegisterAttached("IsDigitOnly",
typeof(bool), typeof(DigitsOnlyBehavior),
new PropertyMetadata(false, OnIsDigitOnlyChanged));
private static void OnIsDigitOnlyChanged(object sender, DependencyPropertyChangedEventArgs e)
{
// ignoring error checking
var textBox = (TextBox)sender;
var isDigitOnly = (bool)(e.NewValue);
if (isDigitOnly)
textBox.PreviewTextInput += BlockNonDigitCharacters;
else
textBox.PreviewTextInput -= BlockNonDigitCharacters;
}
private static void BlockNonDigitCharacters(object sender, TextCompositionEventArgs e)
{
e.Handled = e.Text.Any(ch => !Char.IsDigit(ch));
}
}
Comments
To get the most basic example working, you will need to add the following assemblies as a minimum:
* PresentationCore.dll
* PresentationFramework.dll
* System.Xaml.dll
* WindowsBase.dll
Here is a simple, one line example:
PanelManager.DisplayWpfElement(new TextBlock { Text = "LINQPad" });
*Note: You will also need to add the System.Windows.Controls namespace to make sure the compiler can find the TextBlock class. Press the F4 key to bring up the Query Properties dialog where you can add assemblies on one tab, and namespaces on the other.
The error: Line number "1" and line item "47" of "The unknown member '{clr-namespace: LINQPad.UserQuery DigitsOnlyBehavior.IsDigitOnly}" can not be set. ".
I assume that the clr-namespace is specified incorrectly.
Oskar
I've tried:
xmlns:local='clr-namespace:LINQPad.UserQuery'
and many reasonable variations.
http://forum.linqpad.net/discussion/52/using-namespace-in-linqpad-code-c-program-mode
You will need to specify an assembly, too:
context.XmlnsDictionary.Add ("beh", "clr-namespace:Foo;assembly=LINQPadQuery");
To make this work, go to Edit | Preferences | Advanced > Execution, and set "Allow LINQPad to access internal types of other assemblies" to true. This will force LINQPad to always call the query assembly "LINQPadQuery" rather than giving it a unique name each time.