Constructor | |
Loadmanager | public class LoadManager Initializes a new instance of the object. |
Methods | |
Load | public void Load (string url) Creates a new WebClient object and calls its DownloadStringAsync method. |
Properties | |
Encoding | public int Encoding Indicates which encoding to use when reading data. Important! To work with different encodings, the library MSPToolkit.dll is used. She has already been added to the project. This parameter is optional, but sometimes, for example, to work with the 1251 encoding, it must be used. |
Saveto | public string SaveTo The name of the local file to which the data will be saved. The file is stored in the application’s isolated storage. |
Developments | |
Oncancel | public event System.Action OnCancel Triggered when canceling data download. |
OnError | public event System.Action <Exception> OnError Fails when data loading error. |
Onfinish | public event System.Action OnFinish It works at the end of the download, regardless of whether there were errors or not. Convenient to use to hide the download progress bar. |
Onload | public event System.Action <string> OnLoad It works when the data is successfully loaded. Data comes in the form of a string. |
OnNoNetwork | public event System.Action OnNoNetwork It works in the absence of a network. Of course, if the network is found, but there is no Internet access, then the OnError event will work, with a message that the resource is unavailable. |
OnProgress | public event System.Action <DownloadProgressChangedEventArgs> OnProgress It works when the download progress changes. |
Onstart | public event System.Action OnStart It works at the start of data download. Convenient to use to show the download progress bar. |
Constructor | |
Filemanager | public class FileManager Initializes a new instance of the object. |
Methods | |
Read | public void Read (string FileName) Opens a file for reading. Accepts the file name as a string. |
Save | public void Save (string FileName, string Data) Opens an existing file for writing or creates a new one if it is missing. |
Properties | |
WriteAfter | public string WriteAfter The string that will be added at the end of the data. Not always XML has a format that serializes normally. For example, sometimes there is not enough root node. This problem can be solved more elegantly, but the simplest method is to wrap the data in an additional root node. |
WriteBefore | public string WriteBefore The string that will be added at the beginning of the data (similar to WriteAfter). |
Developments | |
OnReadError | public event System.Action <Exception> OnReadError It works when a file read error occurs. |
OnReadFileMissing | public event System.Action OnReadFileMissing It works in the absence of a file that is trying to open for reading. If this event was intercepted, the OnReadError event will no longer be raised. |
OnReadReady | public event System.Action <StreamReader> OnReadReady It works when the file is open for reading. Sends an open file descriptor. |
OnSaveError | public event System.Action <Exception> OnSaveError Fails when writing a file. |
LoadManager DataLoader = new LoadManager(); DataLoader.OnLoad += new Action<string>(DataLoader_OnLoad); DataLoader.Load(resorce_url); void DataLoader_OnLoad(string data) { try { Deployment.Current.Dispatcher.BeginInvoke( delegate { // do something with data string }); } catch (Exception ex) { Deployment.Current.Dispatcher.BeginInvoke( delegate { MessageBox.Show(ex.Message, "Exception", MessageBoxButton.OK); }); } }
FileManager CacheFile = new FileManager(); CacheFile.OnReadReady += new Action<StreamReader>(File_OnReadOpen); CacheFile.Read(file_name); void File_OnReadOpen(StreamReader Stream) { // do something with file stream }
// public void LoadData() { // FileManager CacheFile = new FileManager(); // CacheFile.OnReadReady += new Action<StreamReader>(File_OnReadOpen); // , CacheFile.OnReadFileMissing += new Action(File_OnReadFileMissing); // CacheFile.Read(Common.Constants.ExchangeTmpFile); } // void File_OnReadFileMissing() { // Deployment.Current.Dispatcher.BeginInvoke( delegate { MessageBox.Show("Local file missing, first time application start?", "FileManager OnReadFileMissing exception", MessageBoxButton.OK); }); } // void File_OnReadOpen(StreamReader Stream) { using (XmlReader XmlReader = XmlReader.Create(Stream)) { // XML XmlSerializer DataSerializer = new XmlSerializer(typeof(RatesList)); _RatesList = (RatesList)DataSerializer.Deserialize(XmlReader); Rates = _RatesList.Collection; } }
// public void UpdateData() { // LoadManager DataLoader = new LoadManager(); // DataLoader.OnNoNetwork += new Action(DataLoader_OnNoNetwork); // DataLoader.OnError += new Action<Exception>(DataLoader_OnError); // DataLoader.OnStart += new Action(DataLoader_OnStart); // DataLoader.OnLoad += new Action<string>(DataLoader_OnLoad); // DataLoader.OnFinish += new Action(DataLoader_OnFinish); // , XML // DataLoader.OnSaveTo += new Action<string>(DataLoader_OnSaveTo); // DataLoader.SaveTo = Common.Constants.ExchangeTmpFile; // DataLoader.Load(Common.Constants.ExchangeApiUrl); } // // // , as is void DataLoader_OnSaveTo(string data) { // FileManager CacheFile = new FileManager(); // ( ) CacheFile.WriteBefore = "<Root>"; // ( ) CacheFile.WriteAfter = "</Root>"; // , CacheFile.Save(Common.Constants.ExchangeTmpFile, data); } // void DataLoader_OnNoNetwork() { Deployment.Current.Dispatcher.BeginInvoke( delegate { MessageBox.Show("No network available.", "LoadManager OnNoNetwork exception", MessageBoxButton.OK); }); } // void DataLoader_OnError(Exception e) { Deployment.Current.Dispatcher.BeginInvoke( delegate { MessageBox.Show(e.Message, "LoadManager OnError exception", MessageBoxButton.OK); }); } // void DataLoader_OnStart() { // IsProgressVisible = true; // IsDataLoaded = false; } // , void DataLoader_OnFinish() { // IsProgressVisible = false; } // , void DataLoader_OnLoad(string data) { try { Deployment.Current.Dispatcher.BeginInvoke( delegate { // LoadData(); // LastUpdate = DateTime.Now; }); } catch (Exception ex) { Deployment.Current.Dispatcher.BeginInvoke( delegate { MessageBox.Show(ex.Message, "LoadManager OnLoad outer exception", MessageBoxButton.OK); }); } }
public partial class MainPage : PhoneApplicationPage { public MainPage() { InitializeComponent(); // DataContext = App.MainViewModel; // Loaded += new RoutedEventHandler(MainPage_Loaded); } // void MainPage_Loaded(object sender, RoutedEventArgs e) { // , UI Deployment.Current.Dispatcher.BeginInvoke( delegate { // App.MainViewModel.UpdateData(); }); } }
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <ListBox Grid.Row="0" ItemsSource="{Binding Rates}"> <ListBox.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Setter Property="HorizontalContentAlignment" Value="Stretch"/> </Style> </ListBox.ItemContainerStyle> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Margin="0,0,0,17"> <StackPanel Orientation="Horizontal"> <TextBlock Text="1" Style="{StaticResource PhoneTextLargeStyle}" Foreground="{StaticResource PhoneAccentBrush}" /> <TextBlock Text="{Binding Currency}" Style="{StaticResource PhoneTextLargeStyle}" Foreground="{StaticResource PhoneAccentBrush}"/> </StackPanel> <Grid HorizontalAlignment="Stretch"> <Grid.ColumnDefinitions> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/> </Grid.ColumnDefinitions> <StackPanel Grid.Column="0"> <TextBlock Text="Buy" Style="{StaticResource PhoneTextNormalStyle}"/> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Buy}" Style="{StaticResource PhoneTextLargeStyle}"/> <TextBlock Text="UAH" Style="{StaticResource PhoneTextLargeStyle}" Opacity="0.5" /> </StackPanel> </StackPanel> <StackPanel Grid.Column="1"> <TextBlock Text="Sale" Style="{StaticResource PhoneTextNormalStyle}" /> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Sale}" Style="{StaticResource PhoneTextLargeStyle}"/> <TextBlock Text="UAH" Style="{StaticResource PhoneTextLargeStyle}" Opacity="0.5"/> </StackPanel> </StackPanel> </Grid> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <StackPanel Grid.Row="1" Orientation="Horizontal" Margin="0,12,0,0"> <TextBlock Text="Last update:" Style="{StaticResource PhoneTextNormalStyle}" /> <TextBlock Text="{Binding LastUpdate}" Style="{StaticResource PhoneTextAccentStyle}" /> </StackPanel> </Grid>
Source: https://habr.com/ru/post/165521/
All Articles