-
Notifications
You must be signed in to change notification settings - Fork 14
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
Usability issues #14
Comments
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:
And the binding is:
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: Expand for a cybernetically enhanced button + helpersextension Button {
convenience init(
content: @autoclosure @escaping () -> String,
onClick: (() -> ())? = nil
) {
self.init()
self.updateContent(content)
if let onClick = onClick {
self.click.addHandler { _, _ in onClick() }
}
}
func updateContent(_ content: @escaping () -> String) {
withObservationTracking {
self.content = content()
} onChange: {
// We need to defer updating content since this closure is executed **before** the observed state
// has been updated.
//
// Also, `onChange` is invoked once for each `withObservationTracking` call so we need to
// recursively call `updateContent`.
guard let dispatcherQueue = WinAppSDK.DispatcherQueue.getForCurrentThread() else { return }
let _ = try! dispatcherQueue.tryEnqueue {
self.updateContent(content)
}
}
}
}
// -------------- More helpers for SwiftUI-like experience -------------
extension StackPanel {
convenience init(content: () -> UIElement) {
self.init()
self.children.append(content())
}
}
extension Window {
convenience init(content: (() -> UIElement)? = nil) {
self.init()
if let content = content {
self.content = content()
}
}
}
extension FrameworkElement {
func verticalAlignment(_ alignment: VerticalAlignment) -> Self {
self.verticalAlignment = alignment
return self
}
func horizontalAlignment(_ alignment: HorizontalAlignment) -> Self {
self.horizontalAlignment = alignment
return self
}
} 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 ! |
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.And then I assume that the data binding on the TextBlock control should work in the following way? Or should it be defined like a data source for the item container?
Thank you,
Bogdan
The text was updated successfully, but these errors were encountered: