Thursday, February 19, 2009

Layer patterns

"A pattern that people hear a lot about right now is the Model-View-Controller (MVC) pattern. In the MVC pattern, the model is the data, the view is the user interface, and the controller is the programmatic interface between the view, the model, and the user input. This pattern, however, does not work well in declarative user interfaces like Windows Presentation Foundation (WPF) or Silverlight because the XAML that these technologies use can define some of the interface between the input and the view (because data binding, triggers, and states can be declared in XAML).

Model-View-Presenter (MVP) is another common pattern for layering applications. In the MVP pattern, the presenter is responsible for setting and managing state for a view. Like MVC, MVP does not quite fit the Silverlight 2 model because the XAML might contain declarative data binding, triggers, and state management. So where does that leave us?

Luckily for Silverlight 2, the WPF community has rallied behind a pattern called Model-View-ViewModel (MVVM). This pattern is an adaptation of the MVC and MVP patterns in which the view model provides a data model and behavior to the view but allows the view to declaratively bind to the view model. The view becomes a mix of XAML and C# (as Silverlight 2 controls), the model represents the data available to the application, and the view model prepares the model in order to bind it to the view"
itt..

Tuesday, February 17, 2009

Silverlight: grid dinamikus kezelése

Egy egyszerű lottó generátort készítettem, ahol a Grid celláinak, oszlopainak dinamikus létrehozását akartam szemléltetni:



Tábla generálása:
LottoGrid.Children.Clear();
LottoGrid.RowDefinitions.Clear();
LottoGrid.ColumnDefinitions.Clear();
int sor,oszlop;
if (tipus==LottoTipusok.lotto45)
{
sor = 5;
oszlop = 9;
}
else
{
sor = 6;
oszlop = 15;
}
for (int i = 0; i < sor; i++)
{
RowDefinition rf = new RowDefinition();
LottoGrid.RowDefinitions.Add(rf);
}
for (int i = 0; i < oszlop; i++)
{
ColumnDefinition cd = new ColumnDefinition();
LottoGrid.ColumnDefinitions.Add(cd);
}
gombok = new Button[sor * oszlop];
for (int i = 0; i < sor; i++)
{
for (int j = 0; j < oszlop; j++)
{
Button btn = new Button();
int mezo = (i * oszlop) + j + 1;
btn.Content = mezo.ToString();
LottoGrid.Children.Add(btn);
btn.SetValue(Grid.ColumnProperty, j);
btn.SetValue(Grid.RowProperty, i);
gombok[mezo - 1] = btn;
}
}

Majd sorsolás:
Random rnd = new Random();
int sorsol = Talalat();
for (int i = 0; i < gombok.Length; i++)
{
gombok[i].Background = new SolidColorBrush(Colors.White);
gombok[i].BorderThickness = new Thickness(1);
gombok[i].Tag = 0;
}
while (sorsol > 0)
{
int proba = rnd.Next(gombok.Length);
if ((int)gombok[proba].Tag == 0)
{
gombok[proba].Tag = 1;
gombok[proba].Background = new SolidColorBrush(Colors.Red);
gombok[proba].BorderThickness = new Thickness(3);
sorsol--;
}
}