Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 应用程序启动时在C中以编程方式设置XAML数据绑定_C#_Xaml_Listview_Data Binding - Fatal编程技术网

C# 应用程序启动时在C中以编程方式设置XAML数据绑定

C# 应用程序启动时在C中以编程方式设置XAML数据绑定,c#,xaml,listview,data-binding,C#,Xaml,Listview,Data Binding,目前,我有一个简单的Twitter客户端,上面有一个刷新按钮,链接到一个命令,该命令通过列表绑定刷新时间线。目前,我通过以下方式实现这一目标: XAML: ViewModel.cs: class ViewModel : INotifyPropertyChanged { public List<Tweet> Tweets { get; set; } readonly Page page; public ViewModel(Page page) {

目前,我有一个简单的Twitter客户端,上面有一个刷新按钮,链接到一个命令,该命令通过列表绑定刷新时间线。目前,我通过以下方式实现这一目标:

XAML:

ViewModel.cs:

class ViewModel : INotifyPropertyChanged
{
    public List<Tweet> Tweets { get; set; }

    readonly Page page;

    public ViewModel(Page page)
    {
        this.page = page;
        RefreshCommand = new TwitterCommand<object>(OnRefresh);
    }

    public TwitterCommand<object> RefreshCommand { get; set; }

    void OnRefresh(object obj)
    {
        PinAuthorizer auth =
        new PinAuthorizer
        {
            Credentials = new LocalDataCredentials()
        };

        if (auth == null || !auth.IsAuthorized)
        {
            page.Frame.Navigate(typeof(oAuth));
            return;
        }

        var twitterCtx = new TwitterContext(auth);

        var timelineResponse =
            (from tweet in twitterCtx.Status
             where tweet.Type == StatusType.Home && tweet.Count == 200
             select tweet)
            .ToList();

        Tweets =
            (from tweet in timelineResponse
             select new Tweet
             {
                 Name = tweet.User.Name,
                 Text = tweet.Text,
                 ImageUrl = tweet.User.ProfileImageUrl
             })
            .ToList();

        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs("Tweets"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public List<User> Users { get; set; }

}
现在,我想在加载MainPage.xaml.cs时填充此时间线

目前,我在MainPage.xaml.cs上有以下内容:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        DataContext = new ViewModel(this);
        InitializeTimeline();
    }


    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }

    void InitializeTimeline()
    {
        // Get oAuth tokens (They already exist for this example).
        PinAuthorizer auth =
        new PinAuthorizer
        {
            Credentials = new LocalDataCredentials()
        };

        var twitterCtx = new TwitterContext(auth);

        //create list timelineResponse populated of last 200 statuses from users home timeline.

        var timelineResponse =
            (from tweet in twitterCtx.Status
             where tweet.Type == StatusType.Home && tweet.Count == 200
             select tweet)
            .ToList();

        //new list of Tweet (contains strings for Name, Text and ImageUrl)
        List<Tweet> Tweets = new List<Tweet>();

        //Populate list with the data collected from timeline response
        Tweets =
            (from tweet in timelineResponse
             select new Tweet
             {
                 Name = tweet.User.Name,
                 Text = tweet.Text,
                 ImageUrl = tweet.User.ProfileImageUrl
             })
            .ToList();
    }
}
现在我在想最好的方法是在这个列表中循环,并在MainPage.xaml中将这些值分配给它们各自的列表值,但目前我正在努力解决这个问题


非常感谢任何帮助:

如果我理解正确,您只需要在加载主页时显示推文,而不仅仅是在单击刷新按钮时

为此,只需从MainViewModel手动调用ViewModel Refresh命令,如下所示:

((ViewModel)DataContext).RefreshCommand.Execute(null);
这将代替InitializeTimeline调用

然后,refresh命令将加载tweets并更新列表,该列表无论如何都会绑定到ListView


然后就可以去掉InitializeTimeline。

现在,如果使用IoC容器,就可以不用任何代码隐藏,但是只要没有太多代码隐藏,分离级别一般就足够了。追求纯MVVM解决方案有时并不值得:
((ViewModel)DataContext).RefreshCommand.Execute(null);