WPF通过WCF将图像绑定到ViewModel的TileLayoutControl

时间:2014-12-05 13:37:11

标签: wpf image wcf xaml binding

我的系统工作正常。我有一个WCF服务,它给了我ImageUrl,我将这个Image下载到我的项目目录并保存到我的文件系统。然后我想动态地在Devexpress TileLayoutControl中显示这个图像。但是没有在UI上显示任何内容。我也没有收到错误。我试图给像这样的ımage源../MenuPhotos/imagename和这样/ TileExample;组件/ MenuPhotos / imagename

我的代码是这样的;

XAML;

<dxlc:TileLayoutControl Name="TileList" AllowItemMoving="True" ItemsSource="{Binding Items}" ShowLayerSeparators="True" Padding="40,60,40,10" AllowLayerSizing="True" >

    <dxlc:TileLayoutControl.Resources>
        <Style TargetType="dxlc:Tile">
            <Setter Property="BorderBrush" Value="Blue" />
            <!--<Setter Property="Background" Value="{Binding BackgroundColor}" />-->
            <Setter Property="Background" Value="{Binding BackgroundColor}" />
            <Setter Property="Size" Value="{Binding SizeType}" />
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Grid>
                        <Image Stretch="UniformToFill"  HorizontalAlignment="Center" VerticalAlignment="Center">
                                <Image.Source>
                                    <BitmapImage UriSource="/TileExample;component/MenuPhotos/restaurant1.jpg" />
                                </Image.Source>
                            </Image>
                            <Image Stretch="Uniform" Source="{Binding IconImageUrl}" HorizontalAlignment="Center" VerticalAlignment="Center" />
                        </Grid>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Header" Value="{Binding}" />
            <Setter Property="HeaderTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBlock Text="{Binding BackgroundImageUrl}" FontFamily="Segoe UI Light" FontSize="12" />
                    </DataTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="dxlc:TileLayoutControl.IsFlowBreak" Value="{Binding IsFlowBreak}" />
            <Setter Property="dxlc:TileLayoutControl.GroupHeader" Value="Menü" />
            <Setter Property="dxwuin:Navigation.NavigateTo" Value="{Binding PageName}" />
            <Setter Property="dxwuin:Navigation.NavigationParameter" Value="1" />
        </Style>

        <Style TargetType="dxlc:TileGroupHeader">
            <Setter Property="Foreground" Value="#FFFFFFFF" />
        </Style>

    </dxlc:TileLayoutControl.Resources>
</dxlc:TileLayoutControl>

我的视图模型就像这样;

IEnumerable<ImageItemsModel> items;
public GroupedItemsViewModel()
{
}
public IEnumerable<ImageItemsModel> Items
{
    get { return items; }
    private set { SetProperty<IEnumerable<ImageItemsModel>>(ref items, value, "Items"); }
}
public void LoadState(object navigationParameter)
{
    MyServiceclient = new MyServiceclient();
    var items = client.GetAllItems(null);
    string remoteUri = "http://example.com/";
    foreach (var item in items)
    {
        if (item.BackgroundImageUrl == null)
        {
            item.BackgroundImageUrl = "/TileExample;component/Assets/Images/user.jpg";
        }
        else
        {

            string remotefilepath = item.BackgroundImageUrl.Replace("../", "").Replace("//", "/");
            string remotefilename = remotefilepath.Replace("img/integration/Dashboard/bg/", "");
            string myStringWebResource = null;
            using (WebClient myWebClient = new WebClient())
            {
                string path = @"C:\Users\USER\Documents\Visual Studio 2013\Projects\TileExample\TileExample\MenuPhotos\";
                string checkpath = @"C:\Users\USER\Documents\Visual Studio 2013\Projects\TileExample\TileExample\MenuPhotos\" + remotefilename;
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                if (!File.Exists(checkpath))
                {
                    myStringWebResource = remoteUri + remotefilepath;
                    try
                    {
                        string downloadintopath = Path.Combine(path, remotefilename);
                        myWebClient.DownloadFile(myStringWebResource, downloadintopath);
                    }
                    catch { }
                }
            }

            item.BackgroundImageUrl = item.BackgroundImageUrl.Replace("../../img/integration/Dashboard/bg", "../MemberPhotos");
        }

    }

    Items = items;
}
#region INavigationAware Members
public void NavigatedFrom(NavigationEventArgs e)
{
}
public void NavigatedTo(NavigationEventArgs e)
{
    LoadState(e.Parameter);
}
public void NavigatingFrom(NavigatingEventArgs e)
{
}
#endregion

在用户界面中我无法看到我的图像。如果我将图像作为视觉工作室的来源包含它的工作。但我将填充图像作为服务。所以我不能使用这种解决方法。谢谢。

1 个答案:

答案 0 :(得分:0)

您好我最近学到了这个解决方案:

在.NET 4.5中,动态图像路径应该是这样的。

<Image Source="pack://application:,,,/TileExample;component/MenuPhotos/restaurant1.jpg"></Image>

此解决方案适合我。