【问题标题】:Windows Phone Silverlight - setting button IsDisabled if bound data length is 0Windows Phone Silverlight - 如果绑定数据长度为 0,则设置按钮 IsDisabled
【发布时间】:2011-06-24 07:17:25
【问题描述】:

这就是我所拥有的 - 我正在编写一个应用程序,除其他外,它会读取 RSS 提要以获取某个播客的剧集,然后显示每个剧集的标题和描述,并带有“收听”和“观看” “ 按钮。但并非所有剧集都有这两个选项 - 如果其中一个选项不可用,RSS 将返回一个空字符串而不是 URL。因此,我尝试使用可以将 IsDisabled 绑定到的 IValueConverter,如果绑定的数据长度为 0,则返回 true,否则返回 false。目前,我只是在“观看”按钮上进行测试,因为“收听”按钮的绑定几乎相同。

MainPage.xaml.cs 的 sn-p:

using System.Xml.Linq;
using System.Windows.Data;
namespace appname
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();
            WebClient PodcastListDownloader = new WebClient();
            PodcastListDownloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(PodcastListDownloadCompleted);
            PodcastListDownloader.DownloadStringAsync(new Uri("http://domain.tld/mobile_app/podcastfeed"));
        }
        void PodcastListDownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
                return;
            XElement xmlPodcastList = XElement.Parse(e.Result);
            PodcastListBox.ItemsSource = from PodcastEpisode in xmlPodcastList.Descendants("item")
                                   select new PodcastItem
                                   {
                                       title = PodcastEpisode.Element("date").Value + " " + PodcastEpisode.Element("title").Value,
                                       subtitle = PodcastEpisode.Element("subtitle").Value,
                                       description = PodcastEpisode.Element("summary").Value,
                                       audio = PodcastEpisode.Element("audio").Value,
                                       video = PodcastEpisode.Element("video").Value,
                                   };
        }
        private void PlayPodcast(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            Microsoft.Phone.Tasks.MediaPlayerLauncher PodcastPlay = new Microsoft.Phone.Tasks.MediaPlayerLauncher();
            PodcastPlay.Media = new Uri(btn.Tag.ToString());
            PodcastPlay.Show();
        }
}
public class PodcastItem
{
    public string title { get; set; }
    public string description { get; set; }
    public string audio { get; set; }
    public string video { get; set; }
    public string subtitle { get; set; }
}
public class StringLengthVisibilityConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null || value.ToString().Length == 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

MainPage.xaml 的 sn-p:

<phone:PhoneApplicationPage 
x:Class="CCoFnow.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="800" 
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait"  Orientation="Portrait"
shell:SystemTray.IsVisible="False">

<Grid x:Name="LayoutRoot" Background="Transparent">
    <!--Panorama control-->
    <controls:Panorama Title="AppName">
        <controls:Panorama.Background>
            <ImageBrush ImageSource="PanoramaBackground.png"/>
        </controls:Panorama.Background>
        <controls:PanoramaItem Header="Podcast" Foreground="{StaticResource PhoneAccentBrush}">
            <ListBox Margin="0,0,-12,0" ItemsSource="{Binding Items}" Name="PodcastListBox">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,0,0,17" Width="432">
                            <TextBlock Text="{Binding title}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                            <TextBlock Text="{Binding description}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                            <StackPanel Orientation="Horizontal">
                                <Button Content="Listen" Width="215" Tag="{Binding audio}" Click="PlayPodcast"/>
                                <Button Content="Watch" Width="215" Tag="{Binding video}" Click="PlayPodcast" IsEnabled="{Binding video, Converter={StringLengthVisibilityConverter}}"/>
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </controls:PanoramaItem>
    </controls:Panorama>
</Grid>
</phone:PhoneApplicationPage>

但是调试器抛出了两个错误:

1) 标签 'StringLengthVisibilityConverter' 确实 XML 命名空间中不存在 'http://schemas.microsoft.com/winfx/2006/xaml/presentation

2) 类型 'StringLengthVisibilityConverter' 原为 未找到。确认您不是 缺少一个程序集,而这一切 已构建引用的程序集

我将转换器设置为 {StaticResource StringLengthVisibilityConverter}(根据 http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter(v=VS.95).aspx),现在只有一个错误:资源“StringLengthVisibilityConverter”无法解析。有了这个错误,我可以调试(运行)代码,但所有的“监视”按钮仍然处于启用状态。

所以我猜我在错误的命名空间中调用它,但我似乎无法找出正确的命名空间。有人可以指出我正确的方向吗?

谢谢!

编辑:在将这些放在一起的过程中,我意识到我需要以不同的方式执行此操作 - 提要现在具有我可以数据绑定到的附加值。但是,我很确定将来某个时候我会需要这个功能,所以无论如何我都会发布。如果问题有简单的解决方案,请告诉我,以便我下次学习并成功完成!

【问题讨论】:

    标签: silverlight data-binding windows-phone-7 ivalueconverter


    【解决方案1】:

    您引用转换器的方式不太正确。您需要一个可用的转换器实例,例如在页面的Resources 部分:

    <phone:PhoneApplicationPage xmlns:conv="namespace reference for your converter goes here"
                                   ...>
        <phone:PhoneApplicationPage.Resources>
            <conv:StringLengthVisibilityConverter x:Key="Length" />
        </phone:PhoneApplicationPage.Resources>
    

    然后,您使用 StaticResource 引用和您提供给转换器的 x:Key 来引用该转换器。

    <Button Content="Watch" 
            Width="215"
            Tag="{Binding video}"
            Click="PlayPodcast"
            IsEnabled="{Binding video, Converter={StaticResource Length}}"/>
    

    我将把你的方法与使用命令和 MVVM 的讨论留到另一天 :)

    【讨论】:

    • 感谢您的回答 - 很明显我还有很多东西要学,但是当我再次尝试这样做时,这让我有一个地方可以回来。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多