Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# IValueConverter和可见性_C#_.net_Xaml_Windows Rt - Fatal编程技术网

C# IValueConverter和可见性

C# IValueConverter和可见性,c#,.net,xaml,windows-rt,C#,.net,Xaml,Windows Rt,我有一个具有3种通信状态的应用程序:已连接、已断开连接和挂起。通信状态由其他一些参数控制。我想在由IValueConverter控制的屏幕上显示相应的图像。但是我不能让它工作 以下是我的Xaml代码,用于包含3个图像: <Image x:Name="connectedImage" Visibility="{Binding ConnectionWithServerEstablished, Converter={StaticResource communitationStateTo

我有一个具有3种通信状态的应用程序:已连接、已断开连接和挂起。通信状态由其他一些参数控制。我想在由IValueConverter控制的屏幕上显示相应的图像。但是我不能让它工作

以下是我的Xaml代码,用于包含3个图像:

<Image x:Name="connectedImage"  
    Visibility="{Binding ConnectionWithServerEstablished, Converter={StaticResource communitationStateToVisibilityConverter}, ConverterParameter=ConverterParameterConnected}"
    Source="Assets/connected.png"
    Stretch="None"
    HorizontalAlignment="Center" />



<Image x:Name="disconnectedImage"
    Visibility="{Binding ConnectionWithServerEstablished, Converter={StaticResource communitationStateToVisibilityConverter}, ConverterParameter=ConverterParameterDisconnected}"
    Source="Assets/disconnect.png"
    Stretch="None"
    HorizontalAlignment="Center" />


<Image x:Name="pendingImage"
    Visibility="{Binding ConnectionWithServerEstablished, Converter={StaticResource communitationStateToVisibilityConverter}, ConverterParameter=ConverterParameterPending}"
    Source="Assets/pending.png"
    Stretch="None"
    HorizontalAlignment="Center" />
最后但并非最不重要的是转换器:

  public class CommunitationStateToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            var result = Visibility.Collapsed;

            if ((string)parameter == "ConverterParameterConnected")
                result = (CommunitationState)value == CommunitationState.Connected ? Visibility.Visible : Visibility.Collapsed;

            if ((string)parameter == "ConverterParameterDisconnected")
                result = (CommunitationState)value == CommunitationState.Disconnected ? Visibility.Visible : Visibility.Collapsed;

            if ((string)parameter == "ConverterParameterPending")
                result = (CommunitationState)value == CommunitationState.Pending ? Visibility.Visible : Visibility.Collapsed;
            Debug.WriteLine("value={0}, parameter={1}, result={2}", value,   (string)parameter, result);
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
数据绑定的工作原理与预期的一样。我知道这是肯定的,因为我有一个文本框绑定了另一个方法,将状态显示为文本。我的转换器被调用,我知道这是肯定的,因为我可以在其中放置一个断点

所以我的转换器有点问题,因为我总是以一个折叠的图像结束

****编辑****

下面是我的debug.Writeline的一些输出

我已连接:

value=Connected, parameter=ConverterParameterConnected, result=Visible
value=Connected, parameter=ConverterParameterDisconnected, result=Collapsed
value=Connected, parameter=ConverterParameterPending, result=Collapsed
我改为待定:

value=Pending, parameter=ConverterParameterConnected, result=Collapsed
value=Pending, parameter=ConverterParameterDisconnected, result=Collapsed
value=Pending, parameter=ConverterParameterPending, result=Visible
我正在等待启动:

value=Connected, parameter=ConverterParameterConnected, result=Visible
value=Connected, parameter=ConverterParameterDisconnected, result=Collapsed
value=Connected, parameter=ConverterParameterPending, result=Collapsed
value=Pending, parameter=ConverterParameterConnected, result=Collapsed
value=Pending, parameter=ConverterParameterDisconnected, result=Collapsed
value=Pending, parameter=ConverterParameterPending, result=Visible

这是正确的,因为我的程序默认为已连接,一秒钟后它意识到它看不到TCP服务器,但仍然可以访问Wifi,所以我将状态更改为挂起

根据您的评论,您的
与服务器建立的连接
属性很可能不会更改以使图像可见,并且/或者当属性值更改时,您不会触发
属性更改
事件

例如,可以通过在从属属性的setter中触发事件来执行此操作:

public bool IRCommandSent
{
    set
    {
        // set the value
        // ...

        // notify event listeneers that the ConnectionWithServerEstablished may have changed
        if (PropertyChanged != null)
        {
             PropertyChanged(this, new PropertyChangedEventArgs("ConnectionWithServerEstablished"));
        }
    }
}

作为DataContext使用的类(您的ViewModel)当然必须为此实现
INotifyPropertyChanged

如果将Visibility=Visible放在XAML中而不是绑定中,图像是否会显示?此外,请尝试使用调试器逐步完成值转换器。我还没有尝试过,但我已尝试对转换器进行硬编码以返回可见性。可见,然后显示图像。调试器在Convert()中显示了哪些参数值?程序将启动并默认为Connected。然后状态变为挂起(wich正确)这就是我在调试器中得到的:value=Connected parameter=ConverterParameterConnected value=Pending parameter disconnected value=Pending parameter=ConverterParameterPending value=Pending parameter=ConverterParameterConnected代码工作正常,因为我把它拉到了一个带有文本块而不是图像的测试应用程序中。正如helb所说,可能的原因是您没有在正确的位置调用PropertyChanged(“ConnectionWithServerEstablisted”),即当任何受影响的属性发生更改时,或者由于某种原因,图像不显示为Visibility=“Visible”。转换器并不是您在我最近的注释中看到的问题,但事实并非如此。OnPropertyChanged的代码:protected void OnPropertyChanged(字符串名称){var handler=this.PropertyChanged;if(handler!=null&&this.dispatcher!=null){dispatcher.BeginInvoke(()=>{handler(this,newpropertyChangedEventArgs(name));});}
public bool IRCommandSent
{
    set
    {
        // set the value
        // ...

        // notify event listeneers that the ConnectionWithServerEstablished may have changed
        if (PropertyChanged != null)
        {
             PropertyChanged(this, new PropertyChangedEventArgs("ConnectionWithServerEstablished"));
        }
    }
}