我正在创建一个UWP应用程序,它应该从API获取一些数据并显示它,这种情况发生在“Refresh()”方法上。
一切正常,直到我尝试进行搜索,即“搜索()”方法。
从MainPage调用“Search()”方法。
public sealed partial class MarvelMenu : Page, INotifyPropertyChanged
{
//Backing field.
private ObservableCollection<Character> _marvelCharacters = new ObservableCollection<Character>();
//Property
public ObservableCollection<Character> MarvelCharacters
{
get { return _marvelCharacters; }
set
{
if (value != _marvelCharacters)
{
_marvelCharacters = value;
//Notify of the change.
NotifyPropertyChanged();
}
}
}
//PropertyChanged event.
public event PropertyChangedEventHandler PropertyChanged;
//PropertyChanged event triggering method.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private ObservableCollection<ComicBook> MarvelComics;
public MarvelMenu()
{
this.InitializeComponent();
MarvelComics = new ObservableCollection<ComicBook>();
}
private async void Page_Loaded(object sender, RoutedEventArgs e)
{
var storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///VoiceCommandDictionary.xml"));
await VoiceCommandDefinitionManager.InstallCommandDefinitionsFromStorageFileAsync(storageFile);
Refresh();
}
public async void Refresh()
{
MyProgressRing.Visibility = Visibility.Visible;
MyProgressRing.IsActive = true;
ErrorTextBlock.Text = "";
MarvelCharacters.Clear();
while (MarvelCharacters.Count < 20)
{
Task t = MarvelFacade.PopulateMarvelCharactersAsync(MarvelCharacters);
await t;
}
try
{
this.MasterListBox.SelectedIndex = 0;
}
catch (Exception)
{
return;
}
MyProgressRing.IsActive = false;
MyProgressRing.Visibility = Visibility.Collapsed;
ErrorTextBlock.Text = MarvelFacade.errorMessage;
var attribute = await MarvelFacade.GetCharacterDataWrapperAsync();
var myAttribute = attribute.attributionText;
try
{
AttributeTextBlock.Text = myAttribute;
}
catch (Exception)
{
return;
}
}
public async void Search(string searchedCharacter)
{
MyProgressRing.Visibility = Visibility.Visible;
MyProgressRing.IsActive = true;
ErrorTextBlock.Text = "";
MarvelCharacters.Clear();
Task t = MarvelFacade.PopulateMarvelCharactersByNameAsync(searchedCharacter, MarvelCharacters);
await t;
MyProgressRing.IsActive = false;
MyProgressRing.Visibility = Visibility.Collapsed;
ErrorTextBlock.Text = MarvelFacade.errorMessage;
}
在调试模式下运行应用程序时,我发现C#代码运行完美,实际上从API检索搜索到的数据,但是没有显示。 即使我看到Visual Studio在该方法中执行了每个步骤,但实际上并没有显示它。
<ListBox Name="MasterListBox"
ScrollViewer.VerticalScrollBarVisibility="Hidden"
ItemsSource="{x:Bind MarvelCharacters}"
Grid.RowSpan="3"
IsHitTestVisible="True"
SelectionChanged="MasterListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate x:DataType="data:Character">
<ListBoxItem Style="{StaticResource ListBoxItemStyle}"
Margin="-12,-11,-12,-13"
IsHitTestVisible="False">
<StackPanel Orientation="Horizontal">
<Ellipse Width="40"
Height="40"
Margin="4">
<Ellipse.Fill>
<ImageBrush ImageSource="{x:Bind thumbnail.small}"/>
</Ellipse.Fill>
</Ellipse>
<TextBlock Text="{x:Bind name}"
VerticalAlignment="Center"
Width="180"
TextTrimming="WordEllipsis"
FontSize="15"
Margin="10,0,0,0"/>
</StackPanel>
</ListBoxItem>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我的ListBox绑定到“MarvelCharacters”,它是一个公共的ObservableCollection属性。所以它在应用程序启动时显示正常,但不会刷新以显示搜索结果。
任何人都可以帮助我,我真的很感激。
答案 0 :(得分:0)
Xaml中的绑定具有称为&#39; Mode&#39;的属性。此属性定义绑定的方式。
有3种模式:OneTime,OneWay,TwoWay
OneTime :使用将让你只将ur数据绑定到ur UI一次,这是在初始化期间。在此之后,UI是独立的。 x:Bind将默认模式设置为OneTime虽然经典绑定({Binding})默认设置为OneWay
OneWay :使用此功能可确保每次数据更新时都更新UI。
{x:Bind name,Mode=OneWay}
{Binding name,Mode=OneWay}
虽然Classic绑定不需要显式声明,但您可以简单地绑定,但对于Compiled Binding,显式声明是必要的。