如何根据另一个动态资源定义动态资源的价值?

发布于 2024-08-12 20:13:19 字数 203 浏览 1 评论 0原文

是否可以从另一个动态资源为一个动态资源分配值?
例如

<sys:Double x:Key="ButtonWidth">48</sys:Double>
<sys:Double x:Key="SmallButtonWidth"> ButtonWidth / 2 </sys:Double>

Is it possible to assign value to a dynamic resource from another dynamic resource?
For example

<sys:Double x:Key="ButtonWidth">48</sys:Double>
<sys:Double x:Key="SmallButtonWidth"> ButtonWidth / 2 </sys:Double>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我一直都在从未离去 2024-08-19 20:13:19

可以使用自定义 MarkupExtension< 来转换值/a>.

例如,

<Window.Resources xmlns:ms="clr-namespace:WpfApplication1.MathShit">
    <sys:Double x:Key="ButtonWidth">48</sys:Double>
    <ms:Calculation x:Key="SmallButtonWidth">
        <ms:Product Operand1="{ms:Value {StaticResource ButtonWidth}}"
                    Operand2="0.5" />
    </ms:Calculation>
</Window.Resources>
namespace WpfApplication1.MathShit
{
    [ContentProperty("Expression")]
    public class Calculation : MarkupExtension
    {
        public IExpression Expression { get; set; }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (Expression == null) throw new Exception("Expression cannot be null.");

            return Expression.CalculateValue();
        }
    }

    [TypeConverter(typeof(ExpressionConverter))]
    public interface IExpression
    {
        double CalculateValue();
    }

    public abstract class BinaryOperation : IExpression
    {
        public IExpression Operand1 { get; set; }
        public IExpression Operand2 { get; set; }

        public double CalculateValue()
        {
            if (Operand1 == null) throw new Exception("Operand1 cannot be null.");
            if (Operand2 == null) throw new Exception("Operand2 cannot be null.");

            return CalculateBinaryOperation();
        }

        protected abstract double CalculateBinaryOperation();
    }
    public class Sum : BinaryOperation
    {
        protected override double CalculateBinaryOperation()
        {
            return Operand1.CalculateValue() + Operand2.CalculateValue();
        }
    }
    public class Product : BinaryOperation
    {
        protected override double CalculateBinaryOperation()
        {
            return Operand1.CalculateValue() * Operand2.CalculateValue();
        }
    }
    public class Value : MarkupExtension, IExpression
    {
        public double? Double { get; set; }

        public Value() { }
        public Value(double @double)
            : this()
        {
            this.Double = @double;
        }

        public double CalculateValue()
        {
            if (Double == null) throw new Exception("Double");

            return Double.Value;
        }

        // Allows easy object instantiation in XAML attributes. (Result of StaticResource is not piped through ExpressionConverter.)
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
    }

    public class ExpressionConverter : DoubleConverter
    {
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            var doubleValue = (double)base.ConvertFrom(context, culture, value);
            return (IExpression)new Value(doubleValue);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            var val = (Value)value;
            return base.ConvertTo(context, culture, val.CalculateValue(), destinationType);
        }
    }
}

通过这个,您可以构建任意表达式树(这比解析数学字符串容易得多,当然如果您不介意麻烦的话,您也可以这样做)。

It is possible to transform a value using a custom MarkupExtension.

e.g.

<Window.Resources xmlns:ms="clr-namespace:WpfApplication1.MathShit">
    <sys:Double x:Key="ButtonWidth">48</sys:Double>
    <ms:Calculation x:Key="SmallButtonWidth">
        <ms:Product Operand1="{ms:Value {StaticResource ButtonWidth}}"
                    Operand2="0.5" />
    </ms:Calculation>
</Window.Resources>
namespace WpfApplication1.MathShit
{
    [ContentProperty("Expression")]
    public class Calculation : MarkupExtension
    {
        public IExpression Expression { get; set; }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (Expression == null) throw new Exception("Expression cannot be null.");

            return Expression.CalculateValue();
        }
    }

    [TypeConverter(typeof(ExpressionConverter))]
    public interface IExpression
    {
        double CalculateValue();
    }

    public abstract class BinaryOperation : IExpression
    {
        public IExpression Operand1 { get; set; }
        public IExpression Operand2 { get; set; }

        public double CalculateValue()
        {
            if (Operand1 == null) throw new Exception("Operand1 cannot be null.");
            if (Operand2 == null) throw new Exception("Operand2 cannot be null.");

            return CalculateBinaryOperation();
        }

        protected abstract double CalculateBinaryOperation();
    }
    public class Sum : BinaryOperation
    {
        protected override double CalculateBinaryOperation()
        {
            return Operand1.CalculateValue() + Operand2.CalculateValue();
        }
    }
    public class Product : BinaryOperation
    {
        protected override double CalculateBinaryOperation()
        {
            return Operand1.CalculateValue() * Operand2.CalculateValue();
        }
    }
    public class Value : MarkupExtension, IExpression
    {
        public double? Double { get; set; }

        public Value() { }
        public Value(double @double)
            : this()
        {
            this.Double = @double;
        }

        public double CalculateValue()
        {
            if (Double == null) throw new Exception("Double");

            return Double.Value;
        }

        // Allows easy object instantiation in XAML attributes. (Result of StaticResource is not piped through ExpressionConverter.)
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
    }

    public class ExpressionConverter : DoubleConverter
    {
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            var doubleValue = (double)base.ConvertFrom(context, culture, value);
            return (IExpression)new Value(doubleValue);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            var val = (Value)value;
            return base.ConvertTo(context, culture, val.CalculateValue(), destinationType);
        }
    }
}

With this you can build arbitrary expression trees (which is a lot more easy than parsing math strings which you can do as well of course if you do not mind the trouble).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文