Created
November 21, 2017 10:09
-
-
Save mrtank/dc63040dffc5aa3d5abe4dd46cdea51b to your computer and use it in GitHub Desktop.
MVVM example json parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Application x:Class="ExampleMVVM.App" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:local="clr-namespace:ExampleMVVM" > | |
<Application.Resources> | |
</Application.Resources> | |
</Application> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Data; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using System.Windows; | |
namespace ExampleMVVM | |
{ | |
/// <summary> | |
/// Interaction logic for App.xaml | |
/// </summary> | |
public partial class App : Application | |
{ | |
protected override void OnStartup(StartupEventArgs e) | |
{ | |
// composition root | |
MainWindowModel model = new MainWindowModel(new JSONParser("foo.xml")); | |
MainWindowViewModel viewModel = new MainWindowViewModel(model); | |
MainWindow view = new MainWindow {DataContext = viewModel}; | |
base.OnStartup(e); | |
view.ShowDialog(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ExampleMVVM | |
{ | |
public interface IJSONParser | |
{ | |
string GetSomeParam(string attributeName); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ExampleMVVM | |
{ | |
using System; | |
public class JSONParser : IJSONParser | |
{ | |
public JSONParser(string fileName) | |
{ | |
// ... | |
} | |
public string GetSomeParam(string attributeName) | |
{ | |
return "baz"; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="ExampleMVVM.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
xmlns:local="clr-namespace:ExampleMVVM" | |
mc:Ignorable="d" | |
Title="MainWindow" Height="350" Width="525"> | |
<Grid> | |
<Label Content="{Binding SomeParsedText}" /> | |
</Grid> | |
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Data; | |
using System.Windows.Documents; | |
using System.Windows.Input; | |
using System.Windows.Media; | |
using System.Windows.Media.Imaging; | |
using System.Windows.Navigation; | |
using System.Windows.Shapes; | |
namespace ExampleMVVM | |
{ | |
/// <summary> | |
/// Interaction logic for MainWindow.xaml | |
/// </summary> | |
public partial class MainWindow : Window | |
{ | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ExampleMVVM | |
{ | |
public class MainWindowModel | |
{ | |
public MainWindowModel(IJSONParser parser) | |
{ | |
SomeParsedText = parser.GetSomeParam("SomeParam"); | |
} | |
public string SomeParsedText { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ExampleMVVM | |
{ | |
public class MainWindowViewModel | |
{ | |
private readonly MainWindowModel _model; | |
public MainWindowViewModel(MainWindowModel model) | |
{ | |
_model = model; | |
} | |
public string SomeParsedText => _model.SomeParsedText; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment