【问题标题】:WPF returning NULL on some WCF methodsWPF 在某些 WCF 方法上返回 NULL
【发布时间】:2015-11-17 11:45:21
【问题描述】:

我正在将当前的 WinForms 应用程序升级到 WPF 应用程序,但遇到了一个奇怪的问题。

首先,方法 (GetLectures) 正在当前应用程序上运行,所以我知道 WCF 方法没有任何问题。

因此,在我的 WPF 应用程序“ViewModel”类中,我正在调用获取所有讲座的方法 (GetLectures)。

public List<Publish.Lecture> Lecture
{
     get
     {
         return Client.GetLectures(Session).ToList();
     }
}

然后我正在使用我的 DataGrid:

<DataGrid BorderBrush="#e5e5e5" CellStyle="{StaticResource episodeDataGridCell}" RowHeaderWidth="0" GridLinesVisibility="None" Background="Transparent" HorizontalAlignment="Left" Margin="10,24,0,0" ItemsSource="{Binding Lecture}" Grid.Row="1" VerticalAlignment="Top" Height="435" Width="472" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Description}" CanUserResize="False" ClipboardContentBinding="{x:Null}" Header="Episode" Width="150"/>
            <DataGridTextColumn Binding="{x:Null}" CanUserResize="False" ClipboardContentBinding="{x:Null}" Header="Access" Width="150"/>
            <DataGridTextColumn Binding="{x:Null}" CanUserResize="False" ClipboardContentBinding="{x:Null}" Header="Player" Width="150"/>
        </DataGrid.Columns>
</DataGrid>

我在“ViewModel”中收到一个错误,上面写着:

Value cannot be null.

哪个声明没有返回任何内容?

我已尝试在该 WCF 服务“GetChannel”中调用另一个方法,并且效果很好。

有人有什么想法吗?

【问题讨论】:

  • 将属性绑定到这样的服务似乎是个坏主意。每次使用该属性时都会调用该服务。
  • @DanielA.White 感谢您告诉我,我会调查此事。

标签: c# .net wpf wcf


【解决方案1】:

如果你的方法 GetLectures 没有返回任何东西.. 你没有检查。不要直接进入.ToList(),而是尝试检查返回值是否为null,或者可以使用Linq 检查.Any()

var ABC = Client.GetLectures(Session);

If(ABC != null && ABC.Any())
   return ABC.ToList();

【讨论】:

  • 虽然你不应该在get中写一个服务调用...每次绑定和每次访问这个属性时,代码都会强制一个服务调用..
  • 请使用代码格式化工具通过编辑格式化代码。它使您的答案更清晰、更具可读性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-09
  • 2016-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多