簡體   English   中英

如何基於另一個動態資源定義動態資源的價值?

[英]How to define value of dynamic resource based on another dynamic resource?

是否可以從另一個動態資源向一個動態資源分配值?
例如

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

可以使用自定義MarkupExtension轉換值。

例如

<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);
        }
    }
}

這樣,您就可以構建任意的表達式樹(這比解析數學字符串要容易得多,如果您不介意麻煩的話,解析數學字符串當然也可以做到)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM