2016-02-16 27 views
0

我试图消除所有我的运行时绑定错误,以加快我的应用程序,但我有一些错误,给我麻烦,我无法解决。 我的主要问题在于显示3D内容的用户控件。 我告诉他们在一段代码:在执行时间绑定错误

<Viewport3D xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     x:Name="viewport" ClipToBounds="False" IsHitTestVisible="False"> 
<Viewport3D.Camera> 
    <PerspectiveCamera FieldOfView="45" NearPlaneDistance="0.125" FarPlaneDistance="Infinity" Position="-176.298724337988,214.871512878159,289.862943629443" LookDirection="0.469846310392954,-0.342020143325669,-0.813797681349374" UpDirection="0.171010071662834,0.939692620785908,-0.296198132726024"/> 
</Viewport3D.Camera> 
    <ModelVisual3D> 
    <ModelVisual3D.Content>      
     <Model3DGroup> 
      <Model3DGroup.Transform> 
       <Transform3DGroup> 
        <ScaleTransform3D ScaleZ="{Binding CurrentScale}" ScaleY="1" ScaleX="1"/> 
        <RotateTransform3D> 
         <RotateTransform3D.Rotation> 
          <AxisAngleRotation3D Axis="0,-1,0" Angle="{Binding CurrentAngle}"/> 
         </RotateTransform3D.Rotation> 
        </RotateTransform3D> 
        <MatrixTransform3D Matrix="1,1,-1,0,1,1,-3,0,0,-1,3,0,0,48,32,1"/> 
       </Transform3DGroup> 
      </Model3DGroup.Transform> 
      <GeometryModel3D x:Name="Box02"> 
       <GeometryModel3D.Geometry> 
        <MeshGeometry3D Positions="1 1 1 "/> 
       </GeometryModel3D.Geometry> 
       <GeometryModel3D.Material> 
        <DiffuseMaterial Brush="{Binding CurrentColor}"/> 
       </GeometryModel3D.Material> 
      </GeometryModel3D> 
      <Model3DGroup> 
       <DirectionalLight Direction="{Binding CurrentLight}" Color="#FFFFFF" x:Name="light"/> 
      </Model3DGroup> 
     </Model3DGroup> 
    </ModelVisual3D.Content>        
</ModelVisual3D> 
</Viewport3D> 

我的绑定有以下几种,我已经得到了在MSDN的类型(连在绑定) https://msdn.microsoft.com/en-us/library/system.windows.media.media3d.directionallight.direction(v=vs.110).aspx 类型:System.Windows.Media.Media3D。的Vector3D

public System.Windows.Media.Media3D.Vector3D CurrentLight 
{ 
    get { return this.currentLight; } 
    set 
    { 
     this.currentLight = value; 
     this.OnPropertyChanged("CurrentLight"); 
    } 
} 
protected System.Windows.Media.Media3D.Vector3D currentLight; 

https://msdn.microsoft.com/en-us/library/system.windows.media.media3d.scaletransform3d.scalez(v=vs.110).aspx 类型:System.Double

public double CurrentScale 
{ 
    get { return this.currentScale; } 
    set 
    { 
     this.currentScale = value; 
     this.OnPropertyChanged("CurrentScale"); 
    } 
} 
protected double currentScale; 

https://msdn.microsoft.com/en-us/library/system.windows.media.media3d.axisanglerotation3d.angle(v=vs.110).aspx 类型:System.Double

public double CurrentAngle 
{ 
    get { return this.currentAngle; } 
    set 
    { 
     this.currentAngle = value; 
     this.OnPropertyChanged("CurrentAngle"); 
    } 
} 
protected double currentAngle; 

https://msdn.microsoft.com/en-us/library/system.windows.media.media3d.diffusematerial.brush(v=vs.110).aspx 类型:System.Windows.Media.Brush

public System.Windows.Media.Brush CurrentColor 
{ 
    get { return this.currentColor; } 
    set 
    { 
     this.currentColor = value; 
     this.OnPropertyChanged("CurrentColor"); 
    } 
} 
protected System.Windows.Media.Brush currentColor; 

虽然绑定正常工作,出现以下消息在运行时:

System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=CurrentScale; DataItem=null; target element is 'ScaleTransform3D' (HashCode=27183531); target property is 'ScaleZ' (type 'Double') 
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=CurrentAngle; DataItem=null; target element is 'AxisAngleRotation3D' (HashCode=2594575); target property is 'Angle' (type 'Double') 
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=CurrentColor; DataItem=null; target element is 'DiffuseMaterial' (HashCode=7565331); target property is 'Brush' (type 'Brush') 
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=CurrentLight; DataItem=null; target element is 'DirectionalLight' (HashCode=4095240); target property is 'Direction' (type 'Vector3D') 

在Stackoverflow中搜索后,我找到了一个关于使用“回退值”的解决方案?这是最好的解决方案吗? Getting many Binding "Information" in WPF output window

我在另一个线程堆栈溢出看到他们建议使用下面的语句(How to eliminate Binding “Information” in WPF output window - cannot retrieve value using the binding):

System.Diagnostics.PresentationTraceSources.DataBindingSource.Switch.Level = System.Diagnostics.SourceLevels.Critical; 

无论如何,为什么会出现这些错误?我知道我使用的类型是正确的,因为我已经在MSDN提供的链接中检查过它们,它是正确的吗?

谢谢

回答

0

我不明白你是否指定了数据绑定的数据上下文。你有没有把下面的代码添加到你的构造函数中?如果不是,那应该是你的问题。数据绑定需要知道从哪里获得财产。

viewport.DataContext = this; 

我发现this文章在这方面是非常翔实的。特别是本段:

绑定既有源也有目标;其中绑定框架负责处理来自源和目标(可选)的更改通知,同时保持两者同步。我们的FieldUserControl中的绑定有一个Path值,它指定了目标,但是源是什么? 如果您在XAML中创建了一个绑定,但未指定源(可能是最常见的用例),则源将被设置为已指定绑定的控件的DataContext。 DataContext从可视化树中继承,从每个控件的父级到子级。 DataContext通常设置为视图模型或业务/模型对象,就像我们的情况那样,顶层控件MainPage的DataContext被设置为ModelObject的一个实例。 因此,FieldUserControl及其所有子元素的DataContext也是ModelObject。这就是我们的价值绑定失败的原因。值是FieldUserControl的属性,而不是我们的模型对象。

+0

请改善您的答案,以便OP了解为什么您提供的解决方案将有所帮助。此外,在您发布的链接中,尝试检索相关部分,以备份您的答案。 – aribeiro

+0

正如我在其他答案中所述,将DataContext分配给viewmodel不会在执行时间中删除绑定错误。 –

0

我假设你已经指定的属性在ViewModel中。尝试将您的ViewModel作为资源添加到您的用户控件,并将其设置为您的数据上下文

xmlns:viewModels="clr-namespace:My.App.ViewModels.Test" 

<UserControl.Resources> 
    <viewModels:YourViewModelClass x:Key="myViewModel"/> 
</UserControl.Resources> 

<Grid DataContext="{Binding Source={StaticResource myViewModel}}"> 

    <!-- Your XAML Code Here --> 

</Grid> 
+0

将视图模型分配为DataContext,不会避免执行时间中的数据绑定错误。 我提取代码时忘了添加它。 –