【发布时间】:2012-12-07 07:48:11
【问题描述】:
我试图在我的第一个(非常简单的)WP7 应用程序中使用 MVVM 模式,但我被严重卡住了,现在我只是想在不关心 MVVM 的情况下让它工作。
我的 MainPage 有一个 MainViewModel,它工作正常。我有一个传递一些数据并导航到“详细信息页面”的按钮。我已经设置了一个导航服务来导航到详细信息页面并传递参数,效果很好。我只是无法将数据绑定到视图工作。由于它是一个简单的应用程序,我决定将数据从 DetailsPageVieModel.cs 传递到后面的 DetailsPage.xaml.cs 代码并在那里完成工作。这是 veiw 模型部分的样子。
public override void Initialize(IDictionary<string, string> parameters)
{
DetailsPage dp = new DetailsPage();
//DetailsPage dp = Application.Current.RootVisual as DetailsPage;
base.Initialize(parameters);
parameters.TryGetValue("url", out vidUrl);
dp.LoadVideoData(vidUrl);
}
在我的 DetailsPage.xaml.cs 中,我有以下内容:
public void LoadVideoData(string url)
{
HtmlWeb doc = new HtmlWeb();
doc.LoadAsync("http://mydomain.com/video.php?url=" + url);
doc.LoadCompleted += doc_LoadCompleted;
}
private void doc_LoadCompleted(object sender, HtmlDocumentLoadCompleted e)
{
this.vidTitle.Text = e.Document.GetElementbyId("title").InnerText;
vidId = e.Document.GetElementbyId("youtubeId").InnerText;
this.vidUrl.Source = new Uri("http://mydomain.com/video.php?url=" + vidUrl, UriKind.Absolute);
BitmapImage bi = new BitmapImage();
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("temp.jpg", FileMode.Open, FileAccess.Read))
{
bi.SetSource(fileStream);
this.vidImg.Height = bi.PixelHeight;
this.vidImg.Width = bi.PixelWidth;
}
}
this.vidImg.Source = bi;
}
这里是相关的 DetailsPage.xaml 代码
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel>
<TextBlock x:Name="vidTitle" Canvas.ZIndex="99" />
<TextBlock Canvas.ZIndex="99" Text="Tap on the image to view the video." FontSize="14" Margin="0"/>
<Button Margin="0 -50" Padding="0" BorderThickness="0">
<Image x:Name="vidImg" Height="225" />
</Button>
<StackPanel Canvas.ZIndex="99" Height="516">
<phone:WebBrowser x:Name="vidUrl" IsScriptEnabled="True" Height="516" Margin="0"/>
</StackPanel>
</StackPanel>
</Grid>
我猜问题出在以下
DetailsPage dp = new DetailsPage();
//DetailsPage dp = Application.Current.RootVisual as DetailsPage;
这两行代码都不起作用。第一行正确执行,但页面没有使用正确的数据进行更新。第二行在到达 dp.LoadVideoData(vidUrl); 时给了我一个运行时错误消息。行。
这是我的第一个 Windows Phone 应用程序,如果有人能提供任何帮助,我将不胜感激。
卡马尔
【问题讨论】:
-
MVVM 有很多种。你在使用任何 MVVM 库吗?我试图了解 Initialize 方法的位置,在视图模型中还是在视图中?
-
嗨,布莱恩特,我没有使用任何库。 Initialize 方法在视图模型中。
标签: viewmodel windows-phone code-behind