Open bgbernovici opened 6 months ago
I'm also unable to navigate via frame!
I gave it another try this evening. After reading about property wrappers, I ended up with this:
public class Vehicle: INotifyPropertyChanged {
private var names: String
public var namesHandler: String {
get {return self.names}
set {
self.names = newValue
self._propertyChanged.invoke(self, PropertyChangedEventArgs("namesHandler"))
}
}
@EventSource<PropertyChangedEventHandler> public var propertyChanged: Event<PropertyChangedEventHandler>
}
And the binding is:
var b: Binding = Binding()
b.mode = BindingMode,oneWay
b.source = container.namesHandler
b.updateSourceTrigger = .propertyChanged
try! names.setBinding(TextBlock.textProperty, b)
But it still does not update the TextBlock. I've also tried using PropertyPath after setting the DataContext without any luck. I assume that it stems from the fact that my variables in the Swift class are not bindable.
I would also like to know if there is a way to do navigation using Frame and Page classes.
By the way, you can use Swift's Observation Framework as an alternative to data binding. For example, given the following button that tracks changes to its content:
You can use it like this:
import Foundation
import UWP
import WinAppSDK
import WindowsFoundation
import WinUI
import Observation
@Observable class State {
var counter: Int = 0
}
@main
public class PreviewApp: SwiftApplication {
var state = State()
lazy var window = Window {
StackPanel {
Button(content: "clicked \(self.state.counter) times") {
self.state.counter += 1
}
}
.horizontalAlignment(.center)
.verticalAlignment(.center)
}
override public func onLaunched(_ args: WinUI.LaunchActivatedEventArgs) {
try! window.activate()
}
}
Thank you for the suggestion @ducaale. I will definitely try it out.
Hey, I am also struggling with navigation using Frame. Did anyone figure this out? 😄
Fantastic work, thanks to @ducaale your suggestion work like a charm ! Looking for more examples with helpers for SwiftUI-like experience.
Hi, thank you for sharing these samples. They have been immensely helpful in getting me started with Swift and WinRT. However, I seem to be encountering a few challenges, and I was hoping to get some clarification on certain aspects. Specifically:
TypeName(name: "Microsoft.UI.Xaml.Controls.Page", kind: TypeKind.primitive)
, but that is generic and I want to point it to my derived page, though I am not sure how to achieve that.Can a static resource be represented and named in code without setting it at the Application level with XAML? I will share an example below:
Application.current.resources.insert("pointerEnteredGradientBrush", pointerEnteredGradientBrush)
lazy var controlTemplate = XamlReader.load(""" <ControlTemplate TargetType="GridViewItem" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
""") lazy var itemContainerStyle = { var style = Style() style.targetType = TypeName(name: "Microsoft.UI.Xaml.Controls.GridViewItem", kind: TypeKind.primitive) style.setters.append(Setter(GridViewItem.templateProperty, controlTemplate)) return style }()
public class Vehicle: INotifyPropertyChanged {
var name: String
public var propertyChanged: Event = Event(
add: { in return EventRegistrationToken() },
remove: { in }
)
....
}