小编Imt*_*ath的帖子

从C#访问样式资源-Xamarin.Forms

我正在使用Xamarin.Forms应用程序。我在App.xaml中定义的以下样式

<Application.Resources>
    <ResourceDictionary>
        <Style x:Key="blueButton" TargetType="Button">
            <Setter Property="BackgroundColor"
                    Value="Blue" />
            <Setter Property="TextColor"
                    Value="White" />
        </Style>            
    </ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

当我想使用MainPage.xaml中的样式时,它工作得很好。

<Button x:Name="CreateGridButton" 
            Margin="0,15,0,0"
            Clicked="CreateGridButton_Clicked"
            Text="Create Grid Layout" Style="{StaticResource blueButton}" />
Run Code Online (Sandbox Code Playgroud)

但是,当我要从MainPage.xaml.cs执行相同操作时,它显示错误消息“当前上下文中不存在名称'blueButton'”。

Button createSL = new Button();
        createSL.Text = "Create Stack Layout";
        createSL.Style = (Style)Resources["blueButton"];
Run Code Online (Sandbox Code Playgroud)

我也尝试了以下方法,也显示了相同的错误。

createSL.Style = bluebutton;
Run Code Online (Sandbox Code Playgroud)

根据我的要求,我无法在XAML中创建此按钮。因此,请从后面的代码中帮助我。

c# xaml xamarin xamarin.forms

6
推荐指数
2
解决办法
1705
查看次数

SwiftUI - 使用上下文菜单删除列表中的行 - UI 故障

List在我的SwiftUI View. 我累了添加一个contextMenu删除单个项目的List. 结果如下。

从列表中删除项目

动画不是预期的。该行在移动下一个之前闪烁。如何设置animation.right或类似的东西,以便没有 UI 故障并且看起来像发生在onDelete.

PS:我不能用,onDelete因为在我的应用程序中,左右滑动还有其他功能。

这是代码。


struct ListDelete: View {
    
    @State var cars = ["Tesla", "Mercedes", "Audi", "Tata", "Jaguar"]
    
    var body: some View {
        List(cars, id: \.self) { car in
            Text(car).contextMenu {
                Button(action: {
                    if let index = self.cars.firstIndex(of: car) {
//                        self.cars.remove(at: index)
                        self.cars.remove(atOffsets: [index])
                    }
                }, label: {
                    HStack {
                        Text("Delete")
                        Spacer()
                        Image(systemName: "trash")
                    }
                })
            }
        }
    }
}

Run Code Online (Sandbox Code Playgroud)

用于从数组中删除项目的两种方法导致了相同的行为。

listview ios swift swiftui

5
推荐指数
2
解决办法
1738
查看次数

How to create custom ListStyle in SwiftUI?

I want to make changes to the listItem's background, rowInsets based on multiple conditions. How can I create a struct conforming to the ListStyle protocol?

I tried conforming to it and get the following code blocks. But I've no idea how to proceed from here.


struct CustomListStyle: ListStyle {
    
    static func _makeView<SelectionValue>(value: _GraphValue<_ListValue<CustomListStyle, SelectionValue>>, inputs: _ViewInputs) -> _ViewOutputs where SelectionValue : Hashable {
        
    }
    
    static func _makeViewList<SelectionValue>(value: _GraphValue<_ListValue<CustomListStyle, SelectionValue>>, inputs: _ViewListInputs) -> _ViewListOutputs where SelectionValue : Hashable { …
Run Code Online (Sandbox Code Playgroud)

ios swift swiftui

5
推荐指数
1
解决办法
619
查看次数

如何在路由时在请求中传递参数(使用 Vapor 的服务器端 Swift)

我正在 Swift 中使用 Vapor 框架编写一个 Web 服务。

在我的应用程序中,我有用户模型。以下是我如何为所有用户路由获取请求。

router.get("users") { request in
    return User.query(on: request).all()
}
Run Code Online (Sandbox Code Playgroud)

在本地运行服务器后,为了获取用户,我可以发出如下请求localhost:8080/users

现在,我想向请求添加参数以使用户超过给定年龄。该请求看起来像localhost:8080/users?above_age=25

如何使用Vapor框架在请求中添加参数?我尝试使用可用的文档,但我无法弄清楚。

由于我现在开始使用 Vapor 进行服务器端 Swift,因此对使用 Vapor 3 的资源的任何引用也将对我可能遇到的其他问题有所帮助。谢谢!

swift vapor server-side-swift

4
推荐指数
1
解决办法
1962
查看次数

SwiftUI - 在视图中的任意位置点击时关闭键盘 - 其他交互元素的问题

我有一个TextField和一些可操作的元素,例如ButtonPicker在视图中。当使用在TextField. 使用this question中的答案,我实现了它。然而,问题来自于其他可操作的项目。

当我点击 a 时Button,动作发生但键盘不会消失。与Toggle开关相同。当我点击 SegmentedStyle 的一个部分时Picker,键盘会关闭,但选择器选择不会改变。

这是我的代码。


struct SampleView: View {

    @State var selected = 0
    @State var textFieldValue = ""

    var body: some View {
        VStack(spacing: 16) {
            TextField("Enter your name", text: $textFieldValue)
                .padding(EdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16))
                .background(Color(UIColor.secondarySystemFill))
                .cornerRadius(4)


            Picker(selection: $selected, label: Text(""), content: {
                Text("Word").tag(0)
                Text("Phrase").tag(1)
                Text("Sentence").tag(2)
            }).pickerStyle(SegmentedPickerStyle())            

            Button(action: {
                self.textFieldValue = "button tapped"
            }, label: …
Run Code Online (Sandbox Code Playgroud)

ios swift swiftui

4
推荐指数
4
解决办法
2865
查看次数

如何防止SwiftUI Text在值变化时抖动并格式化小数点两边的双值?

我有一个倒计时器。当数字不断变化时,文字会水平移动,感觉晃动太大了,因为我每0.1秒更新一次计时器。

如何防止文字晃动?这是完整的代码:

struct CountDownTimerView: View {
    
    @ObservedObject var timer = CountdownTimer(seconds: 15)
    
    var body: some View {
        Text(String(format: "%.1f", timer.time))
            .font(Font.system(size: 144).weight(.light))
    }
}

class CountdownTimer: ObservableObject {
    @Published var time: Double
    
    /// time interval in seconds
    let interval = 0.1
    
    lazy var timer = Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { _ in
        self.update()
    }
    
    init(seconds: Double) {
        time = seconds
        timer.fire()
    }
    
    func update() {
        if self.time-interval <= 0 {
            self.time = 0
            timer.invalidate()
        } else {
            self.time -= interval …
Run Code Online (Sandbox Code Playgroud)

double timer swift swiftui

4
推荐指数
1
解决办法
879
查看次数

资源字典Exception - Xamarin.Forms中找不到密钥

我正在创建一个Xamarin.Forms应用程序.我无法从代码背后设置样式.以下代码位于我的页面的构造函数中.

Style greenButton = new Style(typeof(Button))
{
    Setters =
    {
        new Setter{Property = Button.BackgroundColorProperty, Value = Color.Green },
        new Setter{Property = Button.TextColorProperty, Value = Color.Red}
    }
};

Resources = new ResourceDictionary();
Resources.Add(greenButton);

Button createSL = new Button();
createSL.Text = "Create Stack Layout";
createSL.Style = (Style) Resources["greenButton"];
Run Code Online (Sandbox Code Playgroud)

上面的代码给出了此错误消息.

未找到密钥异常表示字典中不存在"greenButton".

但是我已经完成了Xamarin.Forms文档中提到的所有内容.请帮我解决一下!

c# resourcedictionary xamarin xamarin.forms

2
推荐指数
1
解决办法
403
查看次数

SwiftUI 中的实时更新相对时间

在我的 SwiftUI 应用程序中,我显示的相对时间如下。

struct ContentView: View {

    let date: Double = 1589014112

    var body: some View {
        Text(Date(timeIntervalSince1970: date).relativeTime())
    }
}

extension Date {
    func relativeTime(in locale: Locale = .current) -> String {
        let formatter = RelativeDateTimeFormatter()
        formatter.unitsStyle = .full
        return formatter.localizedString(for: self, relativeTo: Date())
    }
}
Run Code Online (Sandbox Code Playgroud)

我从 API 中获得了自 1970 年以来的时间间隔加倍。我想在我的应用中显示相对时间。输出如下。

在此处输入图片说明

视图仅在我重新加载时更新,并且每一秒都停留在“10 秒前”。如何实时更新?我想在当前日期 ( Date()) 更改时更改。

nsdateformatter ios swift swiftui

1
推荐指数
2
解决办法
1259
查看次数