0

다음은 내가 가지고있는 것입니다. - 특정 Podcast의 에피소드를 얻기 위해 RSS 피드를 읽은 다음, 각각을 표시하는 App을 작성하고 있습니다. 에피소드의 제목과 설명을 '듣기'및 '보기'버튼과 함께 표시합니다. 그러나 모든 에피소드에는 두 가지 옵션이 모두있는 것은 아닙니다. RSS는 사용할 수없는 경우 두 옵션 중 하나의 URL 대신 빈 문자열을 반환합니다. 그래서 바인딩 된 데이터 길이가 0이면 true를 반환하고 그렇지 않으면 false를 반환하는 IsDisabled를 바인딩 할 수있는 IValueConverter를 사용하려고합니다. 지금은 바인딩을 "듣기"버튼과 거의 동일하므로 "watch"버튼에서 테스트하고 있습니다.Windows Phone Silverlight - 바인딩 된 데이터 길이가 0 인 경우 IsDisabled 버튼

MainPage.xaml.cs를의 조각 :

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의 조각 :

<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) The tag 'StringLengthVisibilityConverter' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation

2) The type 'StringLengthVisibilityConverter' was not found. Verify that you are not missing an assembly and that all referenced assemblies have been built

I을 대신 변환기를 {StaticResource StringLengthVisibilityConverter}로 설정하고 (http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter(v=VS.95).aspx) 이제 jus가 있습니다. 한 오류 : "StringLengthVisibilityConverter"리소스를 확인할 수 없습니다. 이 오류로 코드를 디버그 (실행) 할 수 있지만 모든 "보기"버튼은 계속 사용 가능하게 유지됩니다.

그래서 나는 틀린 네임 스페이스에서 그것을 호출하고 있다고 생각합니다. 그러나 나는 정확한 것을 알아 내지 못하는 것 같습니다. 누군가 올바른 방향으로 나를 가리킬 수 있습니까?

감사합니다.

편집 :이 작업을 함께 진행하는 과정에서 다르게 처리해야한다는 사실을 깨달았습니다. 이제 피드에 데이터 바인딩 할 수있는 추가 값이 있습니다. 그러나 앞으로 어떤 점에서이 기능이 필요할 것이라고 확신하므로 어쨌든 게시하겠습니다. 질문에 대한 쉬운 해결책이 있다면 다음 번에 그것을 배우고 성공적으로 할 수 있도록 알려주십시오!

답변

1

변환기를 참조하는 방식이 적절하지 않습니다. 사용할 수있는 변환기의 인스턴스가 필요합니다 (예 : somwhere). 페이지의 Resources 섹션 :

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

그런 다음 당신은 당신이 변환기를 준 x:KeyStaticResource 참조를 사용하여 해당 변환기를 참조합니다. 그것이 내가 배울 아직 많이 분명하지만, 이것은 나에게 장소를 제공합니다 -

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

나는 당신의 대답은 또 다른 하루 :)

+0

감사 명령과 MVVM을 사용하여 대 접근 방식의 토론을 떠날거야 다시이 일을하려고 할 때 곧 돌아올 것입니다. – james3mg

관련 문제