使用JSON设计时间Windows应用商店应用

时间:2014-05-31 18:22:33

标签: json windows windows-store-apps design-time-data

我正在尝试使用Blank Windows 8商店App创建一个Windows应用商店应用,但无法获得基本的设计时间。我显然缺少一些简单的东西,但是我所做的研究没有看到差异

这是我的MainPage.xaml

<Page
x:Class="TestHubDataBinding.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestHubDataBinding"
xmlns:data="using:TestHubDataBinding.ViewModels"
xmlns:repo="using:TestHubDataBinding.Repo"
 d:DataContext="{Binding Source={d:DesignData Source=Repo/VacationData.json, IsDesignTimeCreatable=True, Type=data:VacationViewModel}}"
xmlns:viewModel="using:TestHubDataBinding.Repo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
    <Hub x:Name="MainHub" d:DataContext="{ Binding VacationViewModel[0], Source={d:DesignData Source=Repo/VacationData.json, IsDesignTimeCreatable=True, Type=repo:VacationDataSource}}">
        <HubSection x:Name="VacationItemsSection" Width="500" Height="500" x:Uid="VacationItemsSection" Header="Vacations">
            <DataTemplate>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                     <TextBlock Style="{StaticResource SubheaderTextBlockStyle}" Grid.Row="1" Margin="0,10,0,0" TextWrapping ="Wrap"
                               Text="{Binding VacationViewModel[0].Name, Mode=OneWay}"  />

                </Grid>
            </DataTemplate>
        </HubSection>
    </Hub>
</Grid>

这是我的视图模型:

[DataContract]
public class VacationViewModel
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public List<string> VacationsList { get; set; }
}

这是我的JSON:

[ 
{
      "Name":"Virginia Beach",
       "VItems":
       [
           {
               "Name":"Shorts (Plad)","Value":"2"
           }
       ]
}
, {
        "Name":"Orlando Florida",
         "VItems":
         [
             {
                 "Name":"Shorts (Plad)","Value":"2"
             }
         ]
  }
]

这是我的DataSource:

public class VacationDataSource
{
    private readonly ObservableCollection<VacationViewModel> _vacations = new ObservableCollection<VacationViewModel>();

    public ObservableCollection<VacationViewModel> Vacations
    {
        get { return _vacations; }
    }

    public async Task<TModel> ToObject<TModel>()
    {

        var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///DataModel/Repo/VacationData.json"));

        var json = await FileIO.ReadTextAsync(file);

        return Deserialize<TModel>(json);
    }

    public string ToJson<TObject>(TObject model)
    {
        return Serialize(model);
    }

    private static string Serialize<TObject>(TObject model, JsonSerializerSettings settings = null)
    {

        if (settings == null)
        {
            settings = new JsonSerializerSettings();
        }

        return JsonConvert.SerializeObject(model, settings);

    }

    private static TModel Deserialize<TModel>(string jsonString, JsonSerializerSettings settings = null)
    {
        if (settings == null)
        {
            settings = new JsonSerializerSettings();
        }

        return JsonConvert.DeserializeObject<TModel>(jsonString, settings);
    }
}

似乎数据将在运行时返回,但设计时间不想工作。

由于

0 个答案:

没有答案