下载的List C#上的ObservableCollection

本文关键字:上的 ObservableCollection List 下载 | 更新日期: 2023-09-27 18:27:48

我有一个类,可以下载一个包含数据的文本文件。数据排列在一行中,所以我把这一行分开,但这一行在一个列表中。计时器下载此数据(列表)。在我的主页中,我想更新所有绑定,它们使用List索引的某个值。什么是最好的方法。

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;
        }            
    }
}        
}

尝试了不同的方法

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;
        }
    }
  }
}

类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

<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>

有趣的是,当我在onPropertyChanged上设置断点时,我可以在XAML部件上看到数据,但不会显示在视图上(请参阅图片)。

无法添加图片,但此处有链接XAML中断

下载的List C#上的ObservableCollection

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