-
Notifications
You must be signed in to change notification settings - Fork 693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Registering attached DependencyProperty of type DependencyProperty throws exception #9313
Comments
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:
|
Because you haven't provided any information, is the application packaged or unpackaged? Is this in your main executable or is it in a separate component? Is it self contained? If it is unpackaged, not self contained and in an executable, it is highly likely that a dependency property defined as a global variable will fail to initialise. The reasoning behind this is simple, C++. If you clean your project and then rebuild it, look at the source files that the project compiles. An extract from an example project, with WindowsPackageType set to None shows the following files being compiled.
The big problem here is, suppose the dependency property is defined in MainWindow.xaml.cpp, the C++ language doesn't guarantee that MddBootstrapInitialize2 will be called before the dependency property initialisation. As you can imagine, trying to initialise a dependency property that uses the Windows App SDK/WinUI 3 when the Windows App Runtime isn't initialised/referenced will not end well. This wouldn't affect packaged applications that properly reference the Windows App Runtime as a dependency. This also wouldn't affect it when the Windows App Runtime is self contained. Finally, this shouldn't affect a component in a separate .dll file since this would be loaded after the executable has initialised. |
@DarranRowe Thanks for that insight, I've never thought of that. But it's packaged project. winrt::Microsoft::UI::Xaml::DependencyProperty Interpolation::LinearProperty()
{
static auto s_linearProperty =
winrt::Microsoft::UI::Xaml::DependencyProperty::RegisterAttached(
L"Linear",
winrt::xaml_typename<winrt::Microsoft::UI::Xaml::DependencyProperty>(),
winrt::xaml_typename<class_type>(),
{ nullptr }
);
return s_linearProperty;
} But it still exceptions on the register line. |
Well, I also encountered similar problems and spent a lot of effort to solve it. But I haven’t encountered an error like yours. What I met is For you I guess the call of your As @DarranRowe said, you should initialize the static member property with winrt::Microsoft::UI::Xaml::DependencyProperty Interpolation::s_linearProperty = nullptr; Then at the App::App() {
Interpolation::LinearProperty() = winrt::Microsoft::UI::Xaml::DependencyProperty::RegisterAttached(
L"Linear",
winrt::xaml_typename</* The value type of s_linearProperty */>(),
winrt::xaml_typename<WinUI3Example::Interpolation>(),
winrt::Microsoft::UI::Xaml::PropertyMetadata{winrt::box_value(/* The default value of s_linearProperty */)});
InitializeComponent();
} As I written above, another Issuse WindowsAppSDK #3673 suggests intializing attached properties at the constructor of App and before the |
@sxlllslgh I uploaded a repro |
I cloned your repository and did some modifications, now it seems work (at least no exception). The critical problem I think is the linearProperty =
winrt::Microsoft::UI::Xaml::DependencyProperty::RegisterAttached(
L"Linear",
winrt::xaml_typename<bool>(),
winrt::xaml_typename<class_type>(),
{ nullptr }
); The complete code is as follows:
namespace winrt::_6_DependencyPropertyType_CPP::implementation {
struct Interpolation : InterpolationT<Interpolation> {
private:
static winrt::Microsoft::UI::Xaml::DependencyProperty linearProperty;
public:
static void Initialize();
static winrt::Microsoft::UI::Xaml::DependencyProperty LinearProperty() { return linearProperty; }
static winrt::Microsoft::UI::Xaml::DependencyProperty GetLinear(const winrt::Microsoft::UI::Xaml::DependencyObject& target);
static void SetLinear(const winrt::Microsoft::UI::Xaml::DependencyObject& target, const winrt::Microsoft::UI::Xaml::DependencyProperty& value);
};
}
namespace winrt::_6_DependencyPropertyType_CPP::implementation {
winrt::Microsoft::UI::Xaml::DependencyProperty Interpolation::linearProperty = nullptr;
void Interpolation::Initialize() {
linearProperty =
winrt::Microsoft::UI::Xaml::DependencyProperty::RegisterAttached(
L"Linear",
winrt::xaml_typename<bool>(),
winrt::xaml_typename<class_type>(),
{ nullptr }
);
}
winrt::Microsoft::UI::Xaml::DependencyProperty Interpolation::GetLinear(const winrt::Microsoft::UI::Xaml::DependencyObject& target) {
return target.GetValue(linearProperty).as<winrt::Microsoft::UI::Xaml::DependencyProperty>();
}
void Interpolation::SetLinear(const winrt::Microsoft::UI::Xaml::DependencyObject& target, const winrt::Microsoft::UI::Xaml::DependencyProperty& value) {
target.SetValue(linearProperty, value);
}
}
App::App() {
Interpolation::Initialize();
// Other code.
} |
That doesn't solve my issue. I was intended to automatically do a linear interpolation of some dependencyProperty when it's set to a value (hence the class name |
Describe the bug
I have a cases that need to register an attached
DependencyProperty
which accepts a value ofDependencyProperty
. Doing this throwsE_NOTIMPL
on app startup.Steps to reproduce the bug
DependencyObject
, with this idlExpected behavior
No response
Screenshots
NuGet package version
WinUI 3 - Windows App SDK 1.4.4: 1.4.231219000
Windows version
Windows 11 (22H2): Build 22621
Additional context
No response
The text was updated successfully, but these errors were encountered: