Comments (4)
Hi I'm an AI powered bot that finds similar issues based off the issue title.
Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one. Thank you!
Closed similar issues:
- WinUI Compiler Crash (#3781), similarity score: 0.71
Note: You can give me feedback by thumbs upping or thumbs downing this comment.
from microsoft-ui-xaml.
This is how I'm creating the ViewModel:
public static class ViewModelCreator
{
// Compiles at runtime and creates a ViewModel.
// Exception thrown:
// 'System.ArgumentNullException' in System.Private.CoreLib.dll
public static object? CreateAtRuntime()
{
string viewModelCSharpCode = GetViewModelCSharpCode();
Assembly viewModelAssemblly = CreateViewModelAssembly(viewModelCSharpCode);
return viewModelAssemblly.CreateInstance("DynamicViewModel");
}
private static string GetViewModelCSharpCode()
{
return
"""
public class DynamicViewModel : System.ComponentModel.INotifyPropertyChanged
{
private string _someText = string.Empty;
public event System.ComponentModel.PropertyChangedEventHandler? PropertyChanged;
public string SomeText
{
get => _someText;
set
{
if (_someText != value)
{
_someText = value;
PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(SomeText)));
}
}
}
}
""";
}
private static Assembly CreateViewModelAssembly(string viewModelCode)
{
CSharpCompilationOptions cSharpCompilationOptions = new(
outputKind: OutputKind.DynamicallyLinkedLibrary,
nullableContextOptions: NullableContextOptions.Enable);
SyntaxTree viewModelClassSyntaxTree = CSharpSyntaxTree.ParseText(viewModelCode);
CSharpCompilation compilation =
CSharpCompilation.Create(
assemblyName: "SomeAssembly",
options: cSharpCompilationOptions,
references: CreateReferences())
.AddSyntaxTrees(viewModelClassSyntaxTree);
using var stream = new MemoryStream();
EmitResult emitResult = compilation.Emit(stream);
if (emitResult.Success is false)
{
throw new Exception("Failed to compile the view model");
}
_ = stream.Seek(0, SeekOrigin.Begin);
return Assembly.Load(stream.ToArray());
}
private static List<PortableExecutableReference> CreateReferences()
{
string runtimePath = RuntimeEnvironment.GetRuntimeDirectory();
List<PortableExecutableReference> references = new()
{
MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location),
MetadataReference.CreateFromFile(Path.Combine(runtimePath, "System.Runtime.dll")),
MetadataReference.CreateFromFile(typeof(System.ComponentModel.INotifyPropertyChanged).GetTypeInfo().Assembly.Location),
};
return references;
}
}
then in MainPage.xaml.cs:
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
// On WinUI 3, TEST 3 works BUT throws "ArgumentNullException" !
this.DataContext = ViewModelCreator.CreateAtRuntime();
}
and in MainPage.xaml
<StackPanel>
<TextBox Text="{Binding SomeText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Text="{Binding SomeText}" />
</StackPanel>
from microsoft-ui-xaml.
Background:
I'm working on an app that edits JSON files. The app has several pages that let you edit a corresponding part of the JSON. It's not just a TreeView. I need to make each page modifyable (layout, using controls, etc) without recompiling the app. That's why I'm using XamlReader and CSharpCompilation to create pages from text files at runtime.
from microsoft-ui-xaml.
This can also be reproduced with SetBinding
:
var binding = new Binding { Path = new PropertyPath("SomeText"), Mode = BindingMode.TwoWay };
this.SomeTextBox.SetBinding(TextBox.TextProperty, binding);
and the exception is thrown here:
XamlTypeInfo.g.cs
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.UI.Xaml.Markup.Compiler"," 3.0.0.2403")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
internal partial class XamlTypeInfoProvider
{
public global::Microsoft.UI.Xaml.Markup.IXamlType GetXamlTypeByType(global::System.Type type)
{
global::Microsoft.UI.Xaml.Markup.IXamlType xamlType;
lock (_xamlTypeCacheByType)
{
if (_xamlTypeCacheByType.TryGetValue(type, out xamlType))
{
return xamlType;
}
from microsoft-ui-xaml.
Related Issues (20)
- The error "Property Not Found" occurs when using x:DataType in a DataTemplate. HOT 7
- TeachingTip Target Property Doesn't Work in WindowsAppSDK version 1.5 HOT 3
- I'm geting the incorrect point object from from Microsoft.UI.Xaml.Input.PointerRoutedEventArgs.GetCurrentPoint() HOT 2
- The property `FlyoutBase.IsOpen` doesn't raise property changed notification HOT 1
- CoreMessagingXP.dll crash HOT 1
- FileOpenPicker, FolderPicker and FolderPicker not available in partial trust/AppContainer HOT 3
- Exception thrown by ShowPrintUIForWindowAsync method on Windows Server 2019. HOT 3
- Proposal: Allow disabling AutoSuggestBox TextChanged event delay / disable debounce
- Keyboard accelerator in NavigationViewItem fires ItemInvoked three times HOT 1
- [NumberBox] Binding, TwoWay, LostFocust changes the value before losing focus HOT 1
- AppWindow.ResizeClient() Problems HOT 2
- TypeDescriptor issue in .NET8 HOT 2
- Text rendering performance is embarrassingly bad HOT 4
- Dropdowns do not respond in WebView 2 HOT 1
- Bug: Right aligned ComboBox popup drastically shifts to the right on opened HOT 4
- Question: How to reorder item in ListView/GridView when it itself is clickable HOT 1
- HTML select popup not following webview2 HOT 1
- [GridView] Narrator won't announce type of item in GridView HOT 2
- Package.appxmanifest file can't be opened with designer HOT 3
- Editable combo box sets bound value to null when itemsource changes HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from microsoft-ui-xaml.