我正在尝试创建一个从网站获取数据并显示它们的 WP8 应用程序。我选择了全景模板,Visual Studio 创建了一些默认代码。
我想要做的是,如果我更改文本绑定到的变量,文本块会自动更新。但是调用 changeDate() 不会改变 UI。文本框仍然显示“dd.mm.yyyy”。
MainPage.xaml:
<phone:LongListSelector.ListHeaderTemplate>
<DataTemplate>
<Grid Margin="12,0,0,38">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock
Text="{Binding Date}"
Style="{StaticResource PanoramaItemHeaderTextStyle}"
Grid.Row="0">
<TextBlock.DataContext>
<ViewModels:MainViewModel/>
</TextBlock.DataContext>
</TextBlock>
</Grid>
</DataTemplate>
</phone:LongListSelector.ListHeaderTemplate>
.
MainViewModel.cs:
public class MainViewModel : INotifyPropertyChanged
{
[...]
private string _date = "dd.mm.yyyy";
public string Date
{
get
{
return _date;
}
set
{
if (value != _date)
{
_date = value;
NotifyPropertyChanged("Date");
}
}
}
//public void changeDate()
//{
// Date = "fu";
// App.ViewModel.Date = "bar";
//}
**UPDATE 2**
public bool IsDataLoaded
{
get;
private set;
}
public void LoadData()
{
System.Net.WebClient wc = new System.Net.WebClient();
wc.DownloadStringCompleted += wc_DownloadStringCompleted;
wc.DownloadStringAsync(new Uri("somelink"));
}
private void wc_DownloadStringCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e)
{
string s = FilterData(e.Result);
}
private string FilterData(string s)
{
string[] split = System.Text.RegularExpressions.Regex.Split(s, "<tbody>");
s = split[1];
split = System.Text.RegularExpressions.Regex.Split(s, "</tbody>");
s = split[0];
split = System.Text.RegularExpressions.Regex.Split(s, "\r\n");
foreach(string str in split)
{
if (str.Contains("class=\"xl24\""))
{
App.ViewModel.Date = "somedate";
}
}
return s;
}
**END UPDATE 2**
[...]
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
更新 1
MainPage.xaml.cs:
public MainPage()
{
InitializeComponent();
DataContext = App.ViewModel;
}
**UPDATE 2**
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (!App.ViewModel.IsDataLoaded)
{
App.ViewModel.LoadData();
}
}
**END UPDATE 2**
[...]
.
App.xaml.cs:
private static MainViewModel viewModel = null;
public static MainViewModel ViewModel
{
get
{
if (viewModel == null)
viewModel = new MainViewModel();
return viewModel;
}
}
[...]