Skip to content

Commit

Permalink
Fixing code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
einari committed Feb 28, 2024
1 parent c26a142 commit d7c27b9
Show file tree
Hide file tree
Showing 19 changed files with 130 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Another good place were you'd might look at having compiler extensions are the p
Typically you would do the following:


\[code:c#\]
```csharp
public class MyNotifyingObject : INotifyPropertyChanging
{
    public event PropertyChangedEventHandler PropertyChanging;
Expand Down Expand Up @@ -51,22 +51,22 @@ public class MyNotifyingObject : INotifyPropertyChanging
       }
    }
}
\[/code\]
```

A more expressive way could be:

\[code:c#\]
```csharp
public class MyNotifyingObject
{
    public notify string Name { get; set; }
}
\[/code\]
```



If you ever worked with WPF or Silverlight, you know the tedious parts where you want to have DependencyProperties on your controls. The syntax is like this:

\[code:c#\]
```csharp
public static readonly DepedencyProperty MyStringProperty =
                  DependencyProperty.Register("MyString", typeof(string), typeof(ParentType), null);

Expand All @@ -81,13 +81,13 @@ public string MyString
           this.SetValue(MyStringProperty,value);
     }
}
\[/code\]
```

Wouldn't it be a lot more expressive if we could just do:

\[code:c#\]
```csharp
public dependent string MyString { get; set; }
\[/code\]
```

The way a compiler extension would handle this, is to fill in all the code found above to make it all work like if you were to write it all. We would get a more expressive code and actually get rid of potential problems with the literal declaring the name of the property as we do today.
**
Expand Down
8 changes: 4 additions & 4 deletions _posts/2008/11/serialization-in-silverlight/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Anyhow, the XmlSerializer one is familiar with from the desktop framework is not

The result is that I made a couple of helper methods to do it all.

\[code:c#\]
```csharp
        public string Serialize<T>(T data)
        {
            using (var memoryStream = new MemoryStream())
Expand All @@ -36,11 +36,11 @@ The result is that I made a couple of helper methods to do it all.
                return theObject;
            }
        }
\[/code\] 
``` 

A sample with a couple of classes:

\[code:c#\]
```csharp
    public class WorkPosition
    {
        public double PositionSize { get; set; }
Expand Down Expand Up @@ -78,4 +78,4 @@ A sample with a couple of classes:
        }
    }

\[/code\]
```
16 changes: 8 additions & 8 deletions _posts/2008/11/silverlight-extension-methods/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Lately I've been doing quite a bit of control development, both custom controls
**Finding a storyboard for a specific visual state
**When dealing with visual states, I tend to want to modify content of the storyboard programatically or I want on occasion to hook up to the completed event to be notified when the transition to the new state is finished. To do this, we need to get the storyboard. The below code is something I use for UserControls and is relying on the existense of a Control called "LayoutRoot". This could be rewritten  to extend Control and you could work the layoutroot instead of the UserControl.

\[code:c#\]
```csharp
        public static Storyboard GetStoryboardForState(this UserControl control, string stateName)
        {
            Storyboard stateStoryboard = null;
Expand All @@ -30,12 +30,12 @@ Lately I've been doing quite a bit of control development, both custom controls
            }
            return stateStoryboard;
        }
\[/code\]
```

**Hooking up to the storyboard completed event for a transition for a state change
**As mentioned above, I like to hook up to the storyboard completed event to know when a storyboard of a transition between two states is finished. This can be done by using the above code and the following:

\[code:c#\]
```csharp
        public static void AddStateCompletedEventHandler(this UserControl control, string stateName, EventHandler stateChanged)
        {
            Storyboard stateStoryboard = control.GetStoryboardForState(stateName);
Expand All @@ -44,12 +44,12 @@ Lately I've been doing quite a bit of control development, both custom controls
                stateStoryboard.Completed += (s, e) => stateChanged(s, new EventArgs());
            }
        }
\[/code\]
```

**Setting a value for a specific named keyframe within a storyboard
**Storyboards within a visual state is not programatically accessible, even though you through in the x:Name="" attribute. Therefor I created a helper for setting a specific value - this one only supports doubles, seeing that was the only datatype I was using for the controls I was developing. Extending this should be fairly simple.

\[code:c#\]
```csharp
        public static void SetValueForKeyFrame(this Storyboard storyboard, string keyFrameName, double value)
        {
            foreach (var timeline in storyboard.Children)
Expand All @@ -69,11 +69,11 @@ Lately I've been doing quite a bit of control development, both custom controls
                }
            }           
        }
\[/code\]
```

**Convienience methods for changing states for a UserControl
**Instead of working directly with the VisualStateManager, I like to do "this.GoToState(...)" on my UserControls. Again, the below code is for user controls, but could just as easily be for a custom control. Combining all of the above and the code below will give you this convience.
\[code:c#\]
```csharp
        public static void GoToState(this UserControl control, string stateName, EventHandler stateChanged)
        {
            GoToState(control, stateName, true, stateChanged);
Expand Down Expand Up @@ -101,4 +101,4 @@ Lately I've been doing quite a bit of control development, both custom controls
                VisualStateManager.GoToState(control, name, useTransitions);
            }
        }
\[/code\]
```
24 changes: 12 additions & 12 deletions _posts/2008/12/inotifypropertychanged-revisited/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The environments are heavily relying on the objects implementing INotifyProperty
This works out fine, with the exception of we as developers have to plumb in this code in all our objects.
Normally you would write something like :

\[code:c#\]
```csharp
public class Employee : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
Expand All @@ -43,7 +43,7 @@ public class Employee : INotifyPropertyChanged
        }
    }
}
\[/code\]
```

One can easily see that the above code can become quite boring to write over and over again. A solution could be to put the frequently used bits in a base class for all your objects. But this steals inheritance.

Expand All @@ -55,7 +55,7 @@ I started playing again with the problem a bit today and came up with a solution

_**Update: 16th of December 2008, thanks to [Miguel Madero](http://www.miguelmadero.com/)**_ _**for pointing out the problem with value types.**_

\[code:c#\]
```csharp
    public static class NotificationExtensions
    {
        public static void Notify(this PropertyChangedEventHandler eventHandler, Expression<Func<object>> expression)
Expand Down Expand Up @@ -84,11 +84,11 @@ _**Update: 16th of December 2008, thanks to [Miguel Madero](http://www.miguelmad
            }
        }
   }
\[/code\]
```

When having the extension method above within reach, you will get the Notify() extension method for the PropertyChanged event in your class. The usage is then very simple. Lets revisit our Employee class again.

\[code:c#\]
```csharp

public class Employee : INotifyPropertyChanged

Expand Down Expand Up @@ -120,7 +120,7 @@ public class Employee : INotifyPropertyChanged

}

\[/code\]
```


This is a highly reusable and pretty compact technique, and if you're not like me and aren't all that agressive with putting "this." all over the place, it will be even more compact. :)
Expand All @@ -129,7 +129,7 @@ _**Update**_, _**16th of December 2008:**_

Since my original post, I also added a SubscribeToChange() extension method. The reason for this is pretty much that I literally don't like literals and wanted to have the ability to subscribe to changes for a specific property.

\[code:c#\]
```csharp
        public static void SubscribeToChange<T>(this T objectThatNotifies, Expression<Func<object>> expression, PropertyChangedEventHandler<T> handler)
            where T : INotifyPropertyChanged
        {
Expand All @@ -155,19 +155,19 @@ Since my original post, I also added a SubscribeToChange() extension method. The
                        }
                    };
        }
\[/code\]
```

The above code extends classes that implements INotifyPropertyChanged and gives you a syntax like  follows for subscribing to events:

\[code:c#\]
```csharp
myObject.SubscripeToChange(()=>myObject.SomeProperty,SomeProperty\_Changed);
\[/code\]
```

 And then your handler would look like this:

\[code:c#\]
```csharp
private void SomeProperty\_Changed(MyObject myObject)
{
    /\* ... implement something here \*/
}
\[/code\]
```
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ I've grown quite fond of WPF and Silverlight, and find the architecture behind b

Lets say you have a ViewModel object, and on it you have a State property you wish to expose and make visible and bindable in Xaml:

\[code:c#\]
```csharp
public class ViewModel
{
     public ViewState State { get; set; }
}
\[/code\]
```


**DependencyProperty out of the box**
In order for this to become a DependencyProperty and something we can bind against in Xaml, we have to do the following:

\[code:c#\]
```csharp
public class ViewModel
{
     public static readonly DependencyProperty StateProperty =
Expand All @@ -37,7 +37,7 @@ public class ViewModel
         set { this.SetValue(StateProperty,value); }
     }
}
\[/code\]
```

If you wanted to get notified if the property changed from databinding or similar, you would have to specify propertymetadata with a handler as the last argument for the Register method.
Its not too bad, but it is error-prone - there is a literal there specifying the name of the property. This has to match the actual name of the property. Hardly refactorable.
Expand All @@ -48,7 +48,7 @@ What I've created for my project are 3 classes that can be used seperately or to

**DependencyPropertyHelper

**\[code:c#\]
**```csharp
public static class DependencyPropertyHelper
{
    public static DependencyProperty Register<T, TResult>(Expression<Func<T, TResult>> expression)
Expand Down Expand Up @@ -91,12 +91,12 @@ public static class DependencyPropertyHelper
        return prop;
    }
}
\[/code\]
```
The helper have the assumption that any change callback should just set the property value directly.
With this helper, we can at least make our dependency properties refactorable:
\[code:c#\]
```csharp
public class ViewModel
{
     public static readonly DependencyProperty StateProperty =
Expand All @@ -107,13 +107,13 @@ public class ViewModel
         set { this.SetValue(StateProperty,value); }
     }
}
\[/code\]
```

But still, we have the problem with the SetValue() in the property. So we need a second helping hand to make it all tick. Introducing the DependencyPropertyExtensions class:
**
DependencyPropertyExtensions

**\[code:c#\]
**```csharp
public static class DependencyPropertyExtensions
{
    public static void SetValue<T>(this DependencyObject obj, DependencyProperty property, T value)
Expand All @@ -134,11 +134,11 @@ public static class DependencyPropertyExtensions
        return (T)obj.GetValue(property);
    }
}
\[/code\]
```
With this in place, we can do the following:
\[code:c#\]
```csharp
public class ViewModel
{
     public static readonly DependencyProperty StateProperty =
Expand All @@ -149,15 +149,15 @@ public class ViewModel
         set { this.SetValue<ViewState>(StateProperty,value); }
     }
}
\[/code\]
```

Our code will now work as planned, and we can start putting any logic we want in the set method.

**TypeSafeDependencyProperty**
Still, it could get better than this.
The DependencyProperty class can live anywhere, so we can actually wrap it all up in a new class that utilizes the other two classes:

\[code:c#\]
```csharp
public class TypeSafeDependencyProperty<T1,T>
{
    private readonly DependencyProperty \_dependencyProperty;
Expand Down Expand Up @@ -198,10 +198,10 @@ public class TypeSafeDependencyProperty<T1,T>
        return typeSafeProperty;
    }
}
\[/code\]
```

Our property can then be implemented as follows:
\[code:c#\]
```csharp
public class ViewModel
{
     public static readonly TypeSafeDependencyProperty<ViewModel,ViewState> StateProperty =
Expand All @@ -212,7 +212,7 @@ public class ViewModel
         set { StateProperty.SetValue(this,value); }
     }
}
\[/code\]
```


**Conclusion
Expand Down
Loading

0 comments on commit d7c27b9

Please sign in to comment.