سؤال

لدي الكود التالي (تم تغيير أسماء الكائنات ، لذلك تجاهل أخطاء بناء الجملة/الإملائية).

public class ViewModel
{
    ViewModelSource m_vSource;

    public ViewModel(IViewModelSource source)
    {
        m_vSource= source;
        m_vSource.ItemArrived += new Action<Item>(m_vSource_ItemArrived);
    }

    void m_vSource_ItemArrived(Item obj)
    {
        Title = obj.Title;
        Subitems = obj.items;
        Description = obj.Description;
    }

    public void GetFeed(string serviceUrl)
    {
        m_vFeedSource.GetFeed(serviceUrl);
    }

    public string Title { get; set; }
    public IEnumerable<Subitems> Subitems { get; set; }
    public string Description { get; set; }
 }

فيما يلي الرمز الذي لدي في CodeBehind في صفحتي.

ViewModel m_vViewModel;

public MainPage()
{
    InitializeComponent();

    m_vViewModel = new ViewModel(new ViewModelSource());
    this.Loaded += new RoutedEventHandler(MainPage_Loaded);

    this.DataContext = m_vViewModel;
}

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    m_vViewModel.GetItems("http://www.myserviceurl.com");
}

أخيرًا ، إليك عينة من شكل XAML الخاص بي.

<!--TitleGrid is the name of the application and page title-->
<Grid x:Name="TitleGrid" Grid.Row="0">
    <TextBlock Text="My Super Title" x:Name="textBlockPageTitle" Style="{StaticResource PhoneTextPageTitle1Style}"/>
    <TextBlock Text="{Binding Path=Title}" x:Name="textBlockListTitle" Style="{StaticResource PhoneTextPageTitle2Style}"/>
</Grid>

هل هناك أي شيء أفعله خطأ هنا؟

هل كانت مفيدة؟

المحلول

أعتقد أن ViewModel يجب أن تنفذ واجهة inotifyPropertyChanged:

    public virtual event PropertyChangedEventHandler PropertyChanged;
    protected virtual void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

ثم تبدو الممتلكات الخاصة بك هكذا:

    private title;
    public string Title 
    { 
        get
        {
            return this.title;
        }

        set
        {
            if (this.title!= value)
            {
                this.title= value;
                this.RaisePropertyChanged("Title");
            }
        }
    }

ميخائيل

نصائح أخرى

حسنًا ، اذهب ، بعد 10 دقائق من نشره ، اكتشفها.

كنت أفتقد تنفيذ enotifyProperty. شكرا إذا كان أي شخص ينظر إلى هذا.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top