简体   繁体   English

XmlDataProvider刷新源WPF

[英]XmlDataProvider Refresh source WPF

I have a WPF Grid 我有一个WPF网格

<Window x:Class="LabsRSS.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Poperecinii Timur Lab" Height="404" Width="588">
<Grid x:Name="blah">
    <Grid.Resources>
        <XmlDataProvider x:Key="flickrdata" Source="http://api.flickr.com/services/feeds/photos_public.gne?tags=dog&amp;lang=en-us&amp;format=rss_200">
            <XmlDataProvider.XmlNamespaceManager>
                <XmlNamespaceMappingCollection>
                    <XmlNamespaceMapping Prefix="media" Uri="http://search.yahoo.com/mrss/"/>
                </XmlNamespaceMappingCollection>
            </XmlDataProvider.XmlNamespaceManager>
        </XmlDataProvider>
        <DataTemplate x:Key="itemTemplate">
            <Image Width="75" Height="75" Source="{Binding Mode=OneWay, XPath=media:thumbnail/@url}"/>
        </DataTemplate>
        <ControlTemplate x:Key="controlTemplate" TargetType="{x:Type ItemsControl}">
            <WrapPanel IsItemsHost="True" Orientation="Horizontal"/>
        </ControlTemplate>
    </Grid.Resources>
    <ItemsControl
   Width="375"
   ItemsSource="{Binding Mode=Default, Source={StaticResource flickrdata}, XPath=/rss/channel/item}"
   ItemTemplate="{StaticResource itemTemplate}"
   Template="{StaticResource controlTemplate}">
    </ItemsControl>
    <TextBox Height="23" Margin="193,0,213,24" Name="textBox1" VerticalAlignment="Bottom" TextChanged="textBox1_TextChanged" />
</Grid>

What I try to do is to replace the tag with my own input from TextBox. 我想做的是用我自己从TextBox输入的内容替换标记。

 private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
    {
        XmlDataProvider dataProvider = (XmlDataProvider)this.blah.FindResource("flickrdata");
        XmlNamespaceManager xnManager = dataProvider.XmlNamespaceManager;
        string newSource = "http://api.flickr.com/services/feeds/photos_public.gne?tags=flower&amp;lang=en-us&amp;format=rss_200";
        newSource = Regex.Replace(newSource, "(^.*tags=)(.+?)(&amp;.*$)", String.Format("{0}{1}{2}", "$1", textBox1.Text, "$3"));
        dataProvider.Source = new Uri(newSource);
        dataProvider.XmlNamespaceManager = xnManager;
        dataProvider.Refresh();

    }

Now the uri seems to be set good but the dataProvider is not refreshing the content, how can I do it? 现在uri似乎设置好了,但是dataProvider没有刷新内容,我该怎么办?

I actually don't think you want to call Refresh... or re -set the XmlNamespaceManger for that matter. 我实际上不认为您想致电Refresh ...或为此重新设置XmlNamespaceManger。 I think the call to Refresh is actually telling the XmlDataProvider to reload the same source it already had. 我认为对Refresh的调用实际上是在告诉XmlDataProvider重新加载它已经拥有的相同源。

When you change the Source that should be all that's necessary to trigger the loading logic. 当您更改Source时,触发加载逻辑就应该是全部。 If you did want to re-set the XmlNamespaceManager you should call DeferRefresh because otherwise the provider will reload on every property change. 如果确实要重新设置XmlNamespaceManager,则应调用DeferRefresh,因为否则提供程序将在每次属性更改时重新加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM