0

我正在尝试用路径填充按钮并获取参数超出范围异常。我在我的视图中绑定到模型上的属性。该属性的代码是:

public PunishmentName Name { get; set; }
public Geometry IconData 
{
    get
    {
        string data = "";
        switch (Name) 
        {
            case PunishmentName.Freeze:
                data = "M 0 0 Q 10 10 20 0";                                               
                break;
        }

        Path path = XamlReader.Load("<Path " + "xmlns=" + "'" + "http://schemas.microsoft.com/winfx/2006/xaml/presentation" + "'" + "  Data= '" + data + " '/>") as Path;
        Geometry geometry = path.Data;
        return geometry;
    }
}

视图中按钮的代码

<Button Grid.Row="1" BorderBrush="Transparent" >
    <Viewbox>
        <Path Data="{Binding Punishment.IconData}"/>
    </Viewbox>
</Button>

我知道这个问题:这里已经有人问过: Windows Phone 7: How to parse Bezier Path string like in XAML?

我尝试将路径放入网格和画布中,结果相同。

但它在那里没有得到解决......而且我是新来的,所以我无法发表评论,也不愿意提出问题作为答案。我退出卡在这里。我可以想到一个解决方法,但这意味着更多的工作和更丑陋的代码......

例外:

{System.Windows.ApplicationUnhandledExceptionEventArgs}
    base: {System.Windows.ApplicationUnhandledExceptionEventArgs}
    ExceptionObject: {System.ArgumentException: Value does not fall within the expected range.
   at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
   at MS.Internal.XcpImports.SetValue(IManagedPeerBase obj, DependencyProperty property, DependencyObject doh)
   at MS.Internal.XcpImports.SetValue(IManagedPeerBase doh, DependencyProperty property, Object obj)
   at System.Windows.DependencyObject.SetObjectValueToCore(DependencyProperty dp, Object value)
   at System.Windows.DependencyObject.SetEffectiveValue(DependencyProperty property, EffectiveValueEntry& newEntry, Object newValue)
   at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
   at System.Windows.DependencyObject.RefreshExpression(DependencyProperty dp)
   at System.Windows.Data.BindingExpression.SendDataToTarget()
   at System.Windows.Data.BindingExpression.SourceAcquired()
   at System.Windows.Data.BindingExpression.System.Windows.IDataContextChangedListener.OnDataContextChanged(Object sender, DataContextChangedEventArgs e)
   at System.Windows.Data.BindingExpression.DataContextChanged(Object sender, DataContextChangedEventArgs e)
   at System.Windows.FrameworkElement.OnDataContextChanged(DataContextChangedEventArgs e)
   at System.Windows.FrameworkElement.OnAncestorDataContextChanged(DataContextChangedEventArgs e)
   at System.Windows.FrameworkElement.NotifyDataContextChanged(DataContextChangedEventArgs e)
   at System.Windows.FrameworkElement.OnAncestorDataContextChanged(DataContextChangedEventArgs e)
   at System.Windows.FrameworkElement.NotifyDataContextChanged(DataContextChangedEventArgs e)
   at System.Windows.FrameworkElement.OnTreeParentUpdated(DependencyObject newParent, Boolean bIsNewParentAlive)
   at System.Windows.DependencyObject.UpdateTreeParent(IManagedPeer oldParent, IManagedPeer newParent, Boolean bIsNewParentAlive, Boolean keepReferenceToParent)
   at MS.Internal.FrameworkCallbacks.ManagedPeerTreeUpdate(IntPtr oldParentElement, IntPtr parentElement, IntPtr childElement, Byte bIsParentAlive, Byte bKeepReferenceToParent, Byte bCanCreateParent)}
    Handled: false

我在带有 Geometry.Parse 的 WPF 应用程序中完成了完全相同的操作,但这仍然不适用于手机...

4

2 回答 2

0

我认为您可能需要提供其余代码以查看哪里出错了。我为 Windows Phone 8.1 WinRT 编写了这段代码,它运行良好:

<Grid>
    <Button Grid.Row="1" BorderBrush="Transparent" >
        <Viewbox>
            <Path Data="{Binding IconData}"/>
        </Viewbox>
    </Button>
</Grid>



public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        this.NavigationCacheMode = NavigationCacheMode.Required;
        Punishment MyPunishment = new Punishment();
        MyPunishment.Name = Punishment.PunishmentName.Freeze;
        this.DataContext = MyPunishment;
    }
}

public class Punishment
{
    public enum PunishmentName { Freeze, Burn };
    public PunishmentName Name { get; set; }
    public Geometry IconData
    {
        get
        {
            string data = "";
            switch (Name)
            {
                case PunishmentName.Freeze:
                    data = "M 0 0 Q 10 10 20 0";
                    break;
            }
            Path path = XamlReader.Load("<Path " + "xmlns=" + "'" + "http://schemas.microsoft.com/winfx/2006/xaml/presentation" + "'" + "  Data= '" + data + " '/>") as Path;
            Geometry geometry = path.Data;
            return geometry;
        }
    }
}
于 2014-09-24T18:56:29.090 回答
0

罗马兹给出了答案。没有必要绑定到几何。一个字符串非常好......我认为在 WPF 中它是必要的,因此很混乱......谢谢你 Romasz

于 2014-09-24T21:24:07.430 回答