简体   繁体   English

下载的List C#上的ObservableCollection

[英]ObservableCollection on a downloaded List C#

I have a class where I download a text file with data. 我有一堂课,在那里我下载了带有数据的文本文件。 The data is aranged in one line so I split this line and but this in a list. 数据排列在一行中,因此我将这一行拆分为一个列表。 A timer Downloads this data (List). 计时器下载此数据(列表)。 In my MainPage I want to update all bindings, which use the some valuesout of the List index. 在我的MainPage中,我想更新所有绑定,这些绑定使用List索引中的某些值。 What would be the best way. 最好的方法是什么。

class ClientRawDataList
{
    public static void Interval()
    {
        DispatcherTimer _timer;
        _timer = new DispatcherTimer();
        _timer.Tick += _timer_Tick;
        _timer.Interval = TimeSpan.FromSeconds(4);
        _timer.Start();
    }

    public static void _timer_Tick(object sender, object e)
    {
        DownloadClientRaw();
    }

    public static async Task<ObservableCollection<string>> DownloadClientRaw()
    {
        ObservableCollection<string> clientrawList = null;
        string Station = (string)Windows.Storage.ApplicationData.Current.LocalSettings.Values["UrlSetting"];
        string Cache = "?nocache=" + DateTime.UtcNow.ToString();
        Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
        {
                string ClientrawFile = "clientraw.txt";

                Uri ClientRaw = new Uri(Station + ClientrawFile + Cache, UriKind.Absolute);
                var httpClient = new HttpClient();

                try
                {
                    var resultRaw = await httpClient.GetStringAsync(ClientRaw);
                    clientrawList = new ObservableCollection<string>(resultRaw.Split(' '));
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }

                httpClient.Dispose();
                return clientrawList;
        }            
    }
}        
}

Tried different Approach 尝试了不同的方法

public sealed partial class MainPage : Page
{
    public DispatcherTimer _timer;

    public MainPage()
    {
        this.InitializeComponent();
        var Setting = Windows.Storage.ApplicationData.Current.LocalSettings;
        Setting.Values["UrlSetting"] = "http://www.flugsportverein-reutte.at/modules/Wetter/";
        //Setting.Values["UrlSetting"] = "http://www.lyndhurst-hill.info/";
        //Interval();
        GetClientraw();
        this.NavigationCacheMode = NavigationCacheMode.Required;
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }

    private void Interval()
    {
        _timer = new DispatcherTimer();
        _timer.Tick += _timer_Tick;
        _timer.Interval = TimeSpan.FromSeconds(5);
        _timer.Start();
    }

    void _timer_Tick(object sender, object e)
    {
       GetClientraw();
    }

    public async Task<ObservableCollection<string>> GetClientraw()
    {
        ClientrawList clientrawList = new ClientrawList();

        string Station = (string)Windows.Storage.ApplicationData.Current.LocalSettings.Values["UrlSetting"];
        string Cache = "?nocache=" + DateTime.UtcNow.ToString();
        {
            string ClientrawFile = "clientraw.txt";
            Uri ClientRaw = new Uri(Station + ClientrawFile + Cache, UriKind.Absolute);

            var httpClient = new HttpClient();
             try
             {
            var resultRaw = await httpClient.GetStringAsync(ClientRaw);
            clientrawList.ClientrawData = new ObservableCollection<string>(resultRaw.Split(' '));

            if (resultRaw == "")
            {
                var messageDialog = new MessageDialog("No main data available");
                await messageDialog.ShowAsync();
            }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            httpClient.Dispose();
            return clientrawList.ClientrawData;
        }
    }
  }
}

Class ClientrawList 类ClientrawList

public class ClientrawList  : INotifyPropertyChanged
{
    private ObservableCollection<string> _clientrawData;
    public ObservableCollection<string> ClientrawData 
    { 
        get 
        { 
            return _clientrawData; 
        } 
        set 
        {
            if (_clientrawData != null)
            {
                _clientrawData.CollectionChanged -= _clientrawData_CollectionChanged;
            }
            _clientrawData = value; 

            if(_clientrawData != null)
            {
                _clientrawData.CollectionChanged += _clientrawData_CollectionChanged;
            }

            onPropertyChanged("ClientrawData");
        } 
    }

    private void _clientrawData_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        onPropertyChanged("ClientrawData");
    }

    public ClientrawList()
    {

    }

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

XAML XAML

<Page
x:Class="Download.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Download"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.DataContext>
    <local:ClientrawList />
</Page.DataContext>
<Grid>
    <GridView>
        <StackPanel>
            <TextBlock  x:Name="txt" Text="{Binding ClientrawData}" Height="224" FontSize="36"/>
        </StackPanel>
    </GridView>

</Grid>
</Page>

The funny part here is that when I set a break point at onPropertyChanged, I can see data on the XAML Part but will not display on the view (see Picture). 有趣的是,当我在onPropertyChanged处设置断点时,我可以在XAML部件上看到数据,但不会显示在视图上(请参见图片)。

Can not add Picture but here is a link XAML break 无法添加图片,但这是XAML中断的链接

如果使用的是WPF,则可以将可观察的集合绑定为GridView或其他控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM