A helpful way to debug HTML canvas while doing a lot of translations, transformations, and so on.
context.fillRect(0, 0, 2, 2);
This line prints a tiny rectangle at the point of zero coordinates.
Random programming things I'd want to remember
A helpful way to debug HTML canvas while doing a lot of translations, transformations, and so on.
context.fillRect(0, 0, 2, 2);
This line prints a tiny rectangle at the point of zero coordinates.
myTextBox.Foreground = new SolidColorBrush(Colors.White);
//YourViewModel.cs: private DelegateCommand yourCommand; public YourPageConstructor() { yourCommand = new DelegateCommand(this.YourMethod); ... } private void YourMethod(object obj) { ...//do something here } public ICommand YourCommand { get { return yourCommand; } } //yourPage.xaml.cs: public yourPage() { InitializeComponent(); this.DataContext = new YourViewModel(); } //yourPage.xaml: <ListBox x:Name="yourDataControl" ...> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <Button Command="{Binding YourCommand}" Content="YourItems" ... /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
//yourPage.xaml, add an x:Name attribute to the content of the opening PhoneApplicationPage tag: <phone:PhoneApplicationPage x:Class="YourProjectName.Views.yourPage" x:Name="nameYourXAMLPageName" ...> //and then change the Button's binding: <ListBox x:Name="yourDataControl" ...> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <Button Command="{Binding ElementName=nameYourXAMLPageName, Path=DataContext.YourCommand}" Content="{Binding YourItems}" ... /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> ...
Summary:
Long version:
I am working on a Windows Phone app. Many developers face this: you code, everything works fine. Then you change a couple of things and boom! you get a cryptic error message. In my case, the phone app kept hitting this code without no apparent reason:private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) { if (System.Diagnostics.Debugger.IsAttached) { // An unhandled exception has occurred; break into the debugger System.Diagnostics.Debugger.Break(); } }
MS.Internal.WrappedException: Error HRESULT E_FAIL has been returned from a call to a COM component. ---> System.Exception: Error HRESULT E_FAIL has been returned from a call to a COM component. at MS.Internal.XcpImports.CheckHResult(UInt32 hr) at MS.Internal.XcpImports.UIElement_Measure_WithDesiredSize(UIElement element, Size availableSize) at System.Windows.UIElement.Measure_WithDesiredSize(Size availableSize) at System.Windows.Controls.VirtualizingStackPanel.MeasureChild(UIElement child, Size layoutSlotSize) at System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight) --- End of inner exception stack trace ---}
<phone:PhoneApplicationPage.Resources> <local:myConverter x:Key="... <DataTemplate x:Key="... ... </DataTemplate </phone:PhoneApplicationPage.Resources>
SELECT a.AField, b.BField, c.CField FROM TableA AS a INNER JOIN (TableB AS b INNER JOIN TableC AS c ON b.BKey = c.BForeignKey) ON a.AKey = b.BForeignKey
TableB AS b INNER JOIN TableC AS c ON b.BKey = c.BForeignKey AND b.BField2 = 'someValue' AND c.CField2 = 'someValue'
TableB AS b INNER JOIN TableC AS c ON b.BKey = c.BForeignKey ... WHERE b.BField2 = 'someValue' AND c.CField2 = 'someValue'
<Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <TextBlock Text="{Binding MyText}" /> <ItemsControl ItemsSource="{Binding MyButtons}" HorizontalAlignment="Center" Padding="0" Grid.Row="1" Style="{StaticResource HorizontalStackPanel}" /> </Grid>
<Style x:Key="HorizontalStackPanel" TargetType="ItemsControl"> <Setter Property="ItemsPanel"> <Setter.Value> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal" /> </ItemsPanelTemplate> </Setter.Value> </Setter> </Style>
public MainPage() { InitializeComponent(); this.DataContext = new ButtonsViewModel(); }
private string _myText; public string MyText { get { return _myText; } set { if (_myText == value) return; _myText = value; this.RaisePropertyChanged("MyText"); } }
//INotifyPropertyChanged implementation public event PropertyChangedEventHandler PropertyChanged; public void RaisePropertyChanged(string propertyName) { PropertyChangedEventHandler handler = this.PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } }
//Buttons property public List<Button> MyButtons { get { List<Button> buttons = new List<Button>(); buttons.Add(MakeAButton("q"); buttons.Add(MakeAButton("w"); buttons.Add(MakeAButton("e"); buttons.Add(MakeAButton("r"); buttons.Add(MakeAButton("s"); return buttons; } } //Button plumbing private Button MakeAButton(string letter) { Button b = new Button { CommandParameter = letter, Content = new TextBlock { Text = letter, FontSize = 24, FontFamily = new System.Windows.Media.FontFamily("Segoe UI Mono") }, Height=80 }; b.Click += b_Click; return b; } //The magic void b_Click(object sender, System.Windows.RoutedEventArgs e) { string a = (sender as Button).CommandParameter.ToString(); SomeText += a; }
<ItemsControl ItemsSource="{Binding MyProperty}" HorizontalAlignment="Center" Padding="0"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal" /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl>
<Style x:Key="HorizontalStackPanel" TargetType="ItemsControl"> <Setter Property="Padding" Value="0" /> <Setter Property="ItemsPanel" Value=...
<Style x:Key="HorizontalStackPanel" TargetType="ItemsControl"> <Setter Property="ItemsPanel"> <Setter.Value> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" /> </ItemsPanelTemplate> </Setter.Value> </Setter> </Style>
for(int i=0;i<myList.Count;i++) await FileIO.AppendTextAsync(myFileName, myList[i]);
The method is of course marked as async, so when my debugger skipped over that line and nothing happened, I though something was wrong with Visual Studio. Then I set the debugger at my AppendTextAsync line and when it stopped, I realized that it was taking forever for the computer to append the items from my list, one-by-one (there were more than ten thousand items, and my computer is not the slowest one out there.
I recently read the book by Marijn Haverbeke called Eloquent JavaScript, which has a pretty heavy (well, in my mind) emphasis on functional programming. I remembered that there is a Join method that I could use to convert my list into a string, and I did it like so:
string result = string.Join(Environment.NewLine, myList.ToArray());and voila! It took a split second.
public class Bookshelf { public int BookshelfID {get;set;} public List<string>BookNames {get;set;} }
CREATE TABLE [dbo].[Bookshelves] ( BookshelfID INT IDENTITY (1,1) NOT NULL , BookNames varchar(MAX)... , PRIMARY KEY CLUSTERED ([BookshelfID] ASC) );
public class EFDbContext : DbContext { public DbSet<Bookshelf> Bookshelves {get;set;} }
SELECT BookshelfID FROM Bookshelves
.Split(new char[]{';'}, StringSplitOptions.RemoveEmptyEntries)
<!--ContentPanel - place additional content here--> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="0.5*"/> <ColumnDefinition Width="0.5*"/> </Grid.ColumnDefinitions> <Button Content="{Binding OneText}" Command="{Binding OneCommand}" IsEnabled="{Binding IsButton1Enabled}" /> <Button Content="{Binding TwoText}" Command="{Binding TwoCommand}" IsEnabled="{Binding IsButton2Enabled}" Grid.Column="1" /> </Grid>
public MainPage() { InitializeComponent(); this.DataContext = new OneViewModel(); }
//ViewModel public class OneViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; Random r; private string _oneText; public string OneText { get { return this._oneText; } set { if (value == this._oneText) return; this._oneText = value; this.OnPropertyChanged("OneText"); } } private string _twoText; public string TwoText { get { return this._twoText; } set { if (value == this._twoText) return; this._twoText = value; this.OnPropertyChanged("TwoText"); } } private bool _isButton1Enabled; public bool IsButton1Enabled { get { return _isButton1Enabled; } set { if (_isButton1Enabled == value) return; _isButton1Enabled = value; this.OnPropertyChanged("IsButton1Enabled"); } } private bool _isButton2Enabled; public bool IsButton2Enabled { get { return _isButton2Enabled; } set { if (_isButton2Enabled == value) return; _isButton2Enabled = value; this.OnPropertyChanged("IsButton2Enabled"); } } private DelegateCommand _oneCommand; private DelegateCommand _twoCommand; //Constructor public OneViewModel() { r = new Random(); //This is my model IsButton1Enabled = true; IsButton2Enabled = false; OneText = r.Next(100).ToString(); TwoText = r.Next(100).ToString(); _oneCommand = new DelegateCommand(this.OneCommandAction); _twoCommand = new DelegateCommand(this.TwoCommandAction); } private void TwoCommandAction(object obj) { OneText = r.Next(100).ToString(); IsButton1Enabled = true; IsButton2Enabled = false; } private void OneCommandAction(object obj) { TwoText = r.Next(100).ToString(); IsButton1Enabled = false; IsButton2Enabled = true; } public ICommand OneCommand { get { return _oneCommand; } } public ICommand TwoCommand { get { return _twoCommand; } } private void OnPropertyChanged(string propertyName) { if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } }
public class DelegateCommand : ICommand { Func<object, bool> canExecute; Action<object> executeAction; public DelegateCommand(Action<object> executeAction) : this(executeAction, null) { } public DelegateCommand(Action<object> executeAction, Func<object, bool> canExecute) { if (executeAction == null) { throw new ArgumentNullException("executeAction"); } this.executeAction = executeAction; this.canExecute = canExecute; } public bool CanExecute(object parameter) //can command execute in its current status? { bool result = true; Func<object, bool> canExecuteHandler = this.canExecute; if (canExecuteHandler != null) { result = canExecuteHandler(parameter); } return result; } public event EventHandler CanExecuteChanged; //occurs when changes occur that affect whether or not the command should execute public void RaiseCanExecuteChanged() { EventHandler handler = this.CanExecuteChanged; if (handler != null) { handler(this, new EventArgs()); } } public void Execute(object parameter) //Method to call when the command is invoked { this.executeAction(parameter); } }
For another tutorial, be sure to check my other article on the subject.