标签: markup-extensions

如何解决MarkupExtension中数据绑定的值?

我已经为基于密钥的字符串翻译做了标记扩展.例

<TextBlock Text="{Translate myKey}" />
Run Code Online (Sandbox Code Playgroud)

现在我希望能够使用嵌套绑定来提供我的密钥.例:

<TextBlock Text="{Translate {Binding KeyFromDataContext}}" />
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我得到一个System.Windows.Data.Binding对象.通过调用ProvideValue并传递ServiceProvider,我可以得到一个BindingExpression:

var binding = Key as Binding;
if (binding == null) {
    return null;
}
var bindingExpression = binding.ProvideValue(_serviceProvider) as BindingExpression;
if (bindingExpression == null) {
    return null;
}
var bindingKey = bindingExpression.DataItem;
Run Code Online (Sandbox Code Playgroud)

我可以得到这个bindingExpression,但DataItem属性为null.我已经像这样测试了我的绑定

<TextBlock Text="{Binding KeyFromDataContext}" />
Run Code Online (Sandbox Code Playgroud)

它工作正常.

有任何想法吗?

c# data-binding wpf markup-extensions

5
推荐指数
1
解决办法
6199
查看次数

获取WPF绑定的值

好吧,我不想在我的MVVM ViewModels中使用一堆ICommands,所以我决定为WPF创建一个MarkupExtension,它为它提供一个字符串(方法的名称),它会返回一个执行该方法的ICommand.

这是一个片段:

public class MethodCall : MarkupExtension
{
    public MethodCall(string methodName)
    {
        MethodName = methodName;
        CanExecute = "Can" + methodName;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        Binding bin = new Binding { Converter = new MethodConverter(MethodName, CanExecute) };

        return bin.ProvideValue(serviceProvider);
    }
}

public class MethodConverter : IValueConverter
{
    string MethodName;
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        //Convert to ICommand
        ICommand cmd = ConvertToICommand();
        if (cmd == null)
            Debug.WriteLine(string.Format("Could not bind to method 'MyMethod' …
Run Code Online (Sandbox Code Playgroud)

c# data-binding wpf mvvm markup-extensions

5
推荐指数
1
解决办法
6313
查看次数

Xamarin.Forms.Xaml.XamlParseException:找不到MarkupExtension

我试图使用Xamarin表单的自定义标记扩展,以便最终实现本地化.我试图深入了解Xamarin形式的例子.

以下是使用扩展名的XAML代码:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:CRI.MAP.Mobile.Views;assembly=CRI.MAP.Mobile"
    x:Class="CRI.MAP.Mobile.Views.CustomerSearchPage">

    <StackLayout>
        <Button 
            Text="{local:Translate Clear}"  
            Command="{Binding ClearCommand}" />
    </StackLayout>
Run Code Online (Sandbox Code Playgroud)

以下是Translation Extension Markup的代码:

using System;
using Xamarin.Forms.Xaml;
using Xamarin.Forms;
using System.Diagnostics;

namespace CRI.MAP.Mobile.Views
{
    // You exclude the 'Extension' suffix when using in Xaml markup
    [ContentProperty ("Text")]
    public class TranslateExtension : IMarkupExtension
    {
        public string Text { get; set; }

        public object ProvideValue (IServiceProvider serviceProvider)
        {
            //if (Text == null)
            //  return null;
            //Debug.WriteLine ("Provide: " + Text);
            // Do your …
Run Code Online (Sandbox Code Playgroud)

c# xaml markup-extensions xamarin.forms

5
推荐指数
1
解决办法
7152
查看次数

可以将TypeConverter用于构造函数参数

我正在尝试写一个像这样的markupextension:

[MarkupExtensionReturnType(typeof(Length))]
public class LengthExtension : MarkupExtension
{
    // adding the attribute like this compiles but does nothing.
    public LengthExtension([TypeConverter(typeof(LengthTypeConverter))]Length value)
    {
        this.Value = value;
    }

    [ConstructorArgument("value")]
    public Length Value { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this.Value;
    }
}
Run Code Online (Sandbox Code Playgroud)

要像这样使用:

<Label Content="{units:Length 1 mm}" />
Run Code Online (Sandbox Code Playgroud)

Errs:

类型"长度"的TypeConverter不支持从字符串转换.

如果我:TypeConverter工作:

  • 把它放在Value房产上并有一个默认的ctor.
  • Length用属性装饰类型.

虽然这可能是x/y,但我不想要任何这些解决方案.

这是转换器的代码:

public class LengthTypeConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        if (sourceType == typeof(string))
        {
            return …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml typeconverter markup-extensions

5
推荐指数
1
解决办法
660
查看次数

在XAML内定义一个集合

我想创建一个绑定到XAML内部定义的字符串的集合。

在WPF中,我可以创建一个ArrayList带有键的资源,准备用作绑定的源(使用StaticResource)。

Xamarin形式有可能吗?

编辑:我已经尝试使用@Stephane Delcroix提出的解决方案来使用此XAML,但是却遇到了未处理的异常:

<?xml version="1.0" encoding="utf-8"?>

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             x:Class="ReferenceApp.Views.GamesPage"
             Title="Games">


    <ContentPage.Resources>
        <x:Array Type="{x:Type sys:String}" x:Key="array">
            <x:String>Hello</x:String>
            <x:String>World</x:String>
        </x:Array>
    </ContentPage.Resources>
    <Grid />

</ContentPage>
Run Code Online (Sandbox Code Playgroud)

但是,如果我删除了 <x:Array >... </x:Array>

我究竟做错了什么?

xaml markup-extensions xamarin.forms

5
推荐指数
2
解决办法
3021
查看次数

MarkupExtension作为Template中的计算属性

有这样的MarkupExtension

public class Extension1 : MarkupExtension
{
    private static int _counter = 0;

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return string.Format("Item {0}", _counter++);
    }
}
Run Code Online (Sandbox Code Playgroud)

和这个XAML

<ListBox>
  <ListBoxItem Content="{my:Extension1}"></ListBoxItem>
  <ListBoxItem Content="{my:Extension1}"></ListBoxItem>
  <ListBoxItem Content="{my:Extension1}"></ListBoxItem>
</ListBox>
Run Code Online (Sandbox Code Playgroud)

我得到这样的清单:

Item 1
Item 2
Item 3
Run Code Online (Sandbox Code Playgroud)

现在我尝试使用此Style生成相同的列表

<Style TargetType="ListBoxItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <TextBox Text="{my:Extension1}"></TextBox>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

并有这样的XAML

<ListBox ItemsSource="{StaticResource data}"></ListBox>
Run Code Online (Sandbox Code Playgroud)

我明白了

Item 0
Item 0
Item 0
Run Code Online (Sandbox Code Playgroud)

所以{my:Extension1}仅评估一次.我可以创建一个将为每个项目评估的计算属性吗?

wpf templates markup-extensions

4
推荐指数
1
解决办法
880
查看次数

数据触发器中的标记扩展

要翻译我的WPF应用程序,我使用Markup扩展,它返回一个Binding对象.这允许我在应用程序运行时切换语言.我像这样使用这个Markup:

<TextBlock Text="{t:Translate 'My String'}" />"
Run Code Online (Sandbox Code Playgroud)

我想通过数据触发器更改按钮文本:

<Button>
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <!-- Custom control template, note the TextBlock formating -->
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid x:Name="ContentHolder">
                            <ContentPresenter TextBlock.Foreground="Red" TextBlock.FontWeight="Bold" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <!-- Custom text triggered by Data Binding... -->
            <Style.Triggers>
                <DataTrigger Binding="{Binding MessageRowButton}" Value="Retry">
                    <Setter Property="Button.Content" Value="{t:Translate Test}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding MessageRowButton}" Value="Acknowledge">
                    <Setter Property="Button.Content" Value="{t:Translate Test}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>
Run Code Online (Sandbox Code Playgroud)

这导致以下异常:

无法在"Setter"类型的"Value"属性上设置"绑定".'绑定'只能在DependencyObject的DependencyProperty上设置.

好的,这对我来说很有意义.我试图在我的资源中定义TextBlock并{StaticResource MyResource}在DataTrigger的Setter Value中使用.但是当我这样做时,我的Button的样式没有正确应用......

如何使用我的标记扩展并更改按钮上的文本而不会破坏在按钮内设置字符串样式的能力?

wpf xaml datatrigger markup-extensions

4
推荐指数
1
解决办法
2059
查看次数

UWP中的自定义MarkupExtension

我想创建自己的MarkupExtension(例如BindingTemplateBinding...)

如何像在WPF中一样为通用应用程序做到这一点?

.net c# xaml markup-extensions uwp

4
推荐指数
1
解决办法
1118
查看次数

.Net 4.5中事件的标记扩展

WPF没有定义用于事件的标记扩展,第三方能够创建可以与事件一起使用的标记扩展.现在,WPF 4.5支持事件的标记扩展.任何人都可以通过优雅的例子帮助如何在.Net 4.5中实现这一目标吗?

wpf events markup-extensions .net-4.5 wpf-4.5

3
推荐指数
1
解决办法
1371
查看次数

WPF - 使用RelativeSource绑定为自定义标记扩展提供设计时值

注意:这不仅仅是关于定制标记扩展.请在标记为重复之前阅读.

我有一个转换器的WPF标记扩展,其中两个如下:

  [ValueConversion(typeof(WindowState), typeof(object))]
  internal class WindowStateToObjectConverter : IValueConverter {
    public WindowStateToObjectConverter() { }

    public WindowStateToObjectConverter(object maximized, object normal) {
      this.maximized = maximized;
      this.normal = normal;
    }

    #region Properties
    #region Maximized Property
    private object maximized;

    public object Maximized {
      get { return maximized; }
      set { maximized = value; }
    }
    #endregion
    #region Normal Property
    private object normal;

    public object Normal {
      get { return normal; }
      set { normal = value; }
    }
    #endregion
    #endregion

    public object Convert(object value, …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml binding markup-extensions

2
推荐指数
1
解决办法
1227
查看次数

头部错误中 img 中的错误开始标记

我正在尝试验证我的标记,以下是我的 html 代码。当我验证它时,说错误

Error: Bad start tag in img in head.
Run Code Online (Sandbox Code Playgroud)

这些是我用来获取错误的标签

<noscript>
  <img height="1" width="1" class="displaynone" src="https://www.facebook.com/tr?id=1007528252627508&ev=PageView&noscript=1">
</noscript>
Run Code Online (Sandbox Code Playgroud)

我认为错误是标签不应包含任何标签,除了 , 和 元素,但我不确定有人能找到确切的问题吗?

html markup html4 markup-extensions

2
推荐指数
1
解决办法
8863
查看次数

WPF 中带有状态的标记扩展

我刚刚发现 WPF 标记扩展实例在控件模板中重用。因此,控件模板的每个副本都获得相同的标记扩展集。

如果您希望扩展程序为其所连接的每个控件保持某种状态,这将不起作用。任何想法如何解决这个问题。

wpf markup-extensions

1
推荐指数
1
解决办法
1239
查看次数