2017-02-27 58 views
2

我正在尝试将设计数据集成到我的UWP应用程序中。 我是按照这个方法从微软步骤:https://docs.microsoft.com/en-us/windows/uwp/data-binding/displaying-data-in-the-designerVisual Studio不显示设计数据

我的问题:数据将不会出现。绑定的只有名字:

My XAML designer

但是我期待更多这样的结果:(旧版本,从运行画面) Expected view

所以我是如何实现的呢?
我决定使用“DesignInstance”,因为已经有了一个ViewModel,它将在以后使用(在运行时当前很好)。

public class MockupModel 
    : WeatherViewModel 
{ 
    public MockupModel() : base() 
    { 
     Random Randomizer = new Random(); 

     CurrentData.PrecipitationIcon = WeatherUnicodeIconLib.Neutral.Snow; 
     CurrentData.PrecipitationValue = 0.234; 
     CurrentData.SunRiseSetIcon = WeatherUnicodeIconLib.Miscellaneous.SunRise; 
     CurrentData.SunRiseSetTime = DateTime.Now; 
     CurrentData.TemperatureUnitIcon = WeatherUnicodeIconLib.Miscellaneous.Celsius; 
     CurrentData.TemperatureValue = -20.75; 
     CurrentData.WeatherStatusDescription = "lorem ipsum"; 
     CurrentData.WeatherStatusIcon = OpenWeatherMapUnicodeStatusIconAdapter.GetStandardIconUnicode(200); 
     CurrentData.WindDirectionDegrees = 240.7; 
     CurrentData.WindSpeedIcon = WeatherUnicodeIconLib.GetBeaufortScaleIcon(3); 

     for (int i = 0; i < 7; i++) 
     { 
      DailyForecastViewModel NewForecastItem = new DailyForecastViewModel(); 

      NewForecastItem.Day = DateTime.Now; 
      NewForecastItem.TemperatureValue = Randomizer.Next(-30, 30); 
      NewForecastItem.WeatherSatusIcon = OpenWeatherMapUnicodeStatusIconAdapter.GetStandardIconUnicode(300); 
      NewForecastItem.WindSpeedIcon = WeatherUnicodeIconLib.GetBeaufortScaleIcon(Randomizer.Next(0, 12)); 

      DailyForecast.Add(NewForecastItem); 
     } 
    } 
} 

之后,MockupViewModel已被添加到XAML代码:
(有无

正因为如此,从原来的视图模型我“MockupViewModel”继承和默认的构造函数创建虚值看看到该用户控件页眉/标签的最后一行)

<UserControl 
x:Class="WeatherControl.WeatherControl" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
xmlns:vm="using:WeatherControl.ViewModel" 
d:DataContext="{d:DesignInstance Type=vm:MockupModel, IsDesignTimeCreatable=True}"> 

<UserControl.Resources> 
    <SolidColorBrush x:Key="FontColor">White</SolidColorBrush> 
    <x:Double x:Key="MainInfoFontSize">90</x:Double> 

    <Style TargetType="TextBlock"> 
     <Setter Property="FontFamily" Value="{StaticResource WeatherIcons}"/> 
     <Setter Property="Foreground" Value="{StaticResource FontColor}"/> 
     <Setter Property="FlowDirection" Value="LeftToRight"/> 
     <Setter Property="FontSize" Value="30"/> 
    </Style> 
</UserControl.Resources> 

<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <StackPanel 
     HorizontalAlignment="Stretch" 
     Padding="20"> 

     <StackPanel 
      HorizontalAlignment="Stretch" 
      Orientation="Horizontal" 
      FlowDirection="RightToLeft"> 
      <TextBlock 
       x:Name="SunSetRiseTime" 
       Text="{Binding Path=CurrentData.SunRiseSetTime}"/> 
      <TextBlock 
       x:Name="SunSetRiseIcon" 
       Text="{Binding Path=CurrentData.SunRiseSetIcon}" 
       Margin="10,0,30,0"/> 

      <TextBlock 
       x:Name="WindDirectionIcon" 
       Text="&#xf0b1;" 
       RenderTransformOrigin="0.5 0.5"> 
       <TextBlock.RenderTransform> 
        <RotateTransform Angle="{Binding Path=CurrentData.WindDirectionDegrees}"/> 
       </TextBlock.RenderTransform> 
      </TextBlock> 
      <TextBlock 
       x:Name="WindBeaufortScaleIcon" 
       Text="{Binding Path=CurrentData.WindSpeedIcon}" 
       Margin="10,0,30,0"/> 

      <TextBlock 
       x:Name="PrecipitationIcon" 
       Text="{Binding Path=CurrentData.PrecipitationIcon}" 
       RenderTransformOrigin="0.5 0.5"/> 
      <TextBlock 
       x:Name="PrecipitationIconValue" 
       Text="{Binding Path=CurrentData.PrecipitationValue}" 
       Margin="10,0,20,0"/> 
     </StackPanel> 

     <StackPanel 
      x:Name="MainInfos" 
      HorizontalAlignment="Stretch" 
      Orientation="Horizontal" 
      FlowDirection="RightToLeft"> 
      <TextBlock 
       x:Name="TemperatureUnitIcon" 
       Text="{Binding Path=CurrentData.TemperatureUnitIcon}" 
       FontSize="{StaticResource MainInfoFontSize}" 
       Margin="0,0,10,0"/> 
      <TextBlock 
       Name="TemperatureValue" 
       Text="{Binding Path=CurrentData.TemperatureValue}" 
       FlowDirection="LeftToRight" 
       FontSize="{StaticResource MainInfoFontSize}" 
       Margin="0,0,40,0"/> 
      <TextBlock 
       x:Name="WeatherStatusIcon" 
       Text="{Binding Path=CurrentData.WeatherStatusIcon}" 
       FontSize="{StaticResource MainInfoFontSize}"/> 
     </StackPanel> 

     <TextBlock 
       x:Name="WeatherDescription" 
       Text="{Binding Path=CurrentData.WeatherStatusDescription}" 
       TextAlignment="Right" 
       Margin="0,0,0,20"/> 

     <ListBox 
      x:Name="DailyForecasts" 
      HorizontalAlignment="Stretch" 
      FlowDirection="RightToLeft" 
      Background="Transparent" 
      ItemsSource="{Binding Path=DailyForecast}"> 
      <ListBox.ItemContainerStyle> 
       <Style TargetType="ListBoxItem"> 
        <Setter Property="Padding" Value="0"/> 

       </Style> 
      </ListBox.ItemContainerStyle> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel 
         x:Name="ForecastDay" 
         Orientation="Horizontal" 
         HorizontalAlignment="Stretch"> 
         <TextBlock 
          x:Name="WindSpeed" 
          TextAlignment="Left" 
          Text="{Binding Path=WindSpeedIcon}" 
          Width="70"/> 
         <TextBlock 
          x:Name="Temperature" 
          TextAlignment="Right" 
          Text="{Binding Path=TemperatureValue}" 
          Width="60"/> 
         <TextBlock 
          x:Name="WeatherIcon" 
          TextAlignment="Center" 
          Text="{Binding Path=WeatherSatusIcon}" 
          Width="100"/> 
         <TextBlock 
          x:Name="DayName" 
          TextAlignment="Left" 
          Text="{Binding Path=Day}" 
          Width="70"/> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </StackPanel> 
</StackPanel> 

您可以阿尔斯o检查我的项目在github上的更详细的代码: https://github.com/Wasserwecken/SmartMirror

我希望你能帮助我,并提前致谢!

回答

2

我随机选择了答案。

  • 设置平台后的 “x86”(64不起作用)
  • 并启用“项目代码” (关于从的Mikael Koskinen的答案)
  • 移动我的“MockupModel “到相同的命名空间作为该用户控件 (关于到https://stackoverflow.com/a/17484110/3885480
  • 我将配置设置为“Release”

    - >它的工作!

enter image description here

所以我检查我的项目设置,以找出原因,并比较了配置
(发布/调试)

  • 检查后 “优化代码”(对于调试配置)
  • 重建(重要)

  • - >没有“无效标记”
  • - >和MockupModel与他的数据显示正确!

enter image description here

这对我的作品在 “2015年VS Comminity UPDATE3” 和 “2017年VS RC”

+0

这个问题在VS2017 15.5.6时仍然存在,请考虑注意问题:https://developercommunity.visualstudio.com/content/problem/63741/在-XAML的设计师无法排解类型 - 从 - - cu.html – Terrence

2

的解决方案是,以确保您使用的x86配置,然后点击名为按钮“启用项目代码”在Visual Studio中:这两个位置

Visual Studio Enable project call

的更多信息:

1)"MSDN: Debugging or Disabling Project Code in XAML Designer"

禁用项目代码可能会导致设计时数据丢失。另一种方法是调试设计器中运行的代码。

2)Enabling/Disabling design data in Visual Studio 2015 Update 1

在该按钮被禁用的情况下,仍然能以某种方式修改UI,因为将要显示的属性的名称,你至少可以设置字体,字体大小,前景色等,这比没有好。

不幸的是,Visual Studio 2015的设计器非常麻烦,所以您最终可能会遇到与我一样的问题:当您切换到x86时,Visual Studio会抱怨“无效标记”。 Blend for Visual Studio具有相同的按钮,如果没有帮助,您可以尝试使用VS2017 RC。

+0

没有什么帮助:(我试着enabe “设计数据”,将配置设置x86,并在2017 RC做这些事情,但是“无效标记”保留:( – Karotte

相关问题