### ViewModel with a get-only property
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Example of a ViewModel with a get-only property. By default, no code is generated for listening to changes of auto-generated get-only properties to optimize performance.
```csharp
class EntityViewModel : INotifyPropertyChanged
{
public EntityViewModel(EntityModel model)
{
Model = model;
}
public EntityModel Model { get; }
}
```
--------------------------------
### Interpolated String with x:Bind
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Create formatted strings dynamically using interpolated strings, starting with '$'. Expressions within curly braces can include formatting specifiers.
```xaml
```
--------------------------------
### Bind to an asynchronous function returning Task
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Bind directly to an asynchronous function that returns a Task. The {x:Bind} markup will await the task to get the result. Fallback values can be provided for the duration of the asynchronous operation.
```xaml
```
```xaml
```
--------------------------------
### Using IsItemsSource for Automatic Data Type Detection
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Illustrates how to use the IsItemsSource property with x:Bind for collection controls. Setting IsItemsSource to true automatically detects the data type of elements within the ItemTemplate, simplifying setup.
```xaml
```
--------------------------------
### Include Raw Assets in .csproj
Source: https://github.com/levitali/compiledbindings/blob/master/source/MAUI/MauiTest/Resources/Raw/AboutAssets.txt
This build action includes all files in the Resources\Raw directory and its subdirectories into the application package. The `LogicalName` ensures the file is placed in the correct relative path within the package.
```xml
```
--------------------------------
### Load Raw Asset at Runtime
Source: https://github.com/levitali/compiledbindings/blob/master/source/MAUI/MauiTest/Resources/Raw/AboutAssets.txt
This C# code demonstrates how to load the content of a raw asset file included in the application package. Ensure the asset file name matches the string passed to `OpenAppPackageFileAsync`.
```csharp
async Task LoadMauiAsset()
{
using var stream = await FileSystem.OpenAppPackageFileAsync("AboutAssets.txt");
using var reader = new StreamReader(stream);
var contents = reader.ReadToEnd();
}
```
--------------------------------
### Binding to Methods with Parameters
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Use a function with any signature and pass parameters directly in XAML when binding to events.
```xaml
```
```csharp
public void Save(bool parameter)
{
}
```
--------------------------------
### Property Paths with x:Bind
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Use x:Bind to directly access properties of the data context. This is suitable for simple data display.
```xaml
```
--------------------------------
### Binary Operators with x:Bind
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Perform arithmetic and comparison operations using binary operators. Parentheses are recommended for clarity in XML.
```xaml
```
--------------------------------
### Declaring CLR-Namespaces with 'using'
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Declare CLR-Namespaces using the 'using' syntax for local declarations. For global declarations that apply to {x:Bind} in other XAML files, use 'global using'.
```xaml
xmlns:local="using:CompiledBindingsDemo"
```
```xaml
xmlns:local="global using:CompiledBindingsDemo"
```
--------------------------------
### Logical Operators with x:Bind
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Combine multiple conditions using logical AND (&&) and OR (||) operators. Ensure correct operator precedence.
```xaml
```
--------------------------------
### Function Calls with x:Bind
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Call instance or static methods directly within x:Bind expressions. Ensure methods are accessible and parameters match.
```xaml
```
--------------------------------
### 'new' Operator with x:Bind
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Instantiate a new object of a specified type within a binding expression. The class must be fully qualified with its namespace.
```xaml
```
--------------------------------
### ViewModel returning a tuple for multiple UI properties
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Define a property that returns a tuple to calculate values for multiple UI properties with a single logic execution. The generated code ensures the property is called only once.
```csharp
public (Brush foreground, Brush background) Colors
{
get
{
switch (this.State)
{
case EntityState.Saved:
return (Brushes.Beige, Brushes.Green);
case EntityState.NotEdited:
return (Brushes.White, Brushes.Black);
...
}
}
}
```
--------------------------------
### Setting Extension Method as Target
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Use an extension method as a target for {x:Bind} or {x:Set} with OneTime and OneWay modes. The extension method must have two parameters: the 'this' parameter of the control and the value to set.
```xaml
xmlns:m="http://compiledbindings.com/"
xmlns:extension="using:CompiledBindingsDemo.Extensions"
...
```
```csharp
public static void SetFocused(this VisualElement visualElement, bool focused)
{
if (focused)
{
visualElement.Focus();
}
}
```
--------------------------------
### Setting x:DataType for x:Bind in WPF
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Demonstrates how to set the data source type for x:Bind in WPF using the mx:DataType attribute when x:DataType is not available. This requires declaring the namespace and marking it as ignorable.
```xaml
xmlns:mx="http://compiledbindings.com/x"
mc:Ignorable="d mx"
xmlns:local="clr-namespace:CompiledBindingsDemo"
mx:DataType="local:MainViewModel"
```
--------------------------------
### Using m: Namespace for Property Binding
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Bind to properties using the m: Namespace to overcome potential build errors, especially when dealing with single quotes in expressions.
```xaml
xmlns:system="using:System"
...
```
```xaml
```
--------------------------------
### Bind to tuple elements for UI properties
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Bind individual UI properties (like Foreground and Background) to the elements of a tuple returned by a ViewModel property. This allows for efficient, single-source calculation of related UI states.
```xaml
```
--------------------------------
### Specifying DataType for a Single x:Bind Expression
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Shows how to set the DataType property for an individual x:Bind expression, allowing for runtime resolution or resetting the data type using {x:Null}.
```xaml
```
--------------------------------
### Binding to Control Events
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Bind to a control's event and execute a function when the event is triggered. The function signature must match the event handler if event parameters are needed.
```xaml
```
```csharp
public void OnDrop(object sender, System.Windows.DragEventArgs e)
{
}
```
--------------------------------
### 'as' Operator with x:Bind
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Use the 'as' operator for type checking and casting. If the cast fails, the expression evaluates to null, preventing runtime errors.
```xaml
```
--------------------------------
### Force generation of property change listening code with [ReadOnly(false)]
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Apply the [ReadOnly(false)] attribute to a get-only property to force the generation of code for listening to its property changes. This is useful if the object returned by the property might change and you need to refresh bindings.
```csharp
[ReadOnly(false)]
public EntityModel Model { get; }
```
--------------------------------
### 'is' Operator with Grouping for Complex Logic
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Use parentheses with the 'is' operator to correctly group expressions and ensure proper operator precedence in complex conditions.
```xaml
(IntProp1 is 0 or 1) and IntProp2 eq 3
```
--------------------------------
### Stop listening to property changes with /. operator
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Use the /. operator to stop listening to property changes for a specific property. This optimizes code generation when the underlying object returned by a property never changes, only its contents do.
```xaml
```
--------------------------------
### 'is' Operator for Type Checking with x:Bind
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Check the type of an object using the 'is' operator, similar to C#'s pattern matching. It supports type names and negation.
```xaml
ObjProp is system:String or not system:Int32
```
--------------------------------
### Coalesce Operator with x:Bind
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Provide a default value for null expressions using the coalesce operator (?:). Values are inferred from the target property's type.
```xaml
```
--------------------------------
### Null Check Operator with x:Bind
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Safely access properties and provide a fallback value if the property is null using the null-coalescing operator (??).
```xaml
```
--------------------------------
### 'is' Operator for Comparisons with x:Bind
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Simplify complex conditional logic using the 'is' operator for equality checks or range comparisons. It allows repeating the left-hand expression only once.
```xaml
IntProp1 is 0 or 1
IntProp1 is ge 0 and le 10
IntProp1 is not gt 0
IntProp1 is not 0
IntProp1 is ne 0
IntProp1 is not eq 0
IntProp1 is gt 0 and le (IntProp2 + 1)
```
--------------------------------
### Force runtime check for INotifyPropertyChanged with \. operator
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Use the \. operator to force a runtime check to see if a property's source object implements INotifyPropertyChanged. This is useful when binding to properties of collections like SelectedItems.Count.
```xaml
```
--------------------------------
### Cast Operator with x:Bind
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Explicitly cast an object to a specific type using the cast operator. The target type must be fully qualified with its namespace.
```xaml
```
--------------------------------
### Disable property change listening with [ReadOnly(true)]
Source: https://github.com/levitali/compiledbindings/blob/master/README.md
Apply the [ReadOnly(true)] attribute to a property to disable code generation for listening to its changes. Use this for properties that are known to never change, optimizing code generation.
```csharp
[ReadOnly(true)]
public string Title => // some logic
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.