我有一个C#模型,通过它的构造函数填充.我想将此对象放在我的_Layout.cshtml中,以便我的Javascript可以将其用作设置.
这是我的模型的样子:
public class SomeConfig
{
public string PropertyA { get; set; }
public string PropertyB { get; set; }
public string PropertyC { get; set; }
public SomeConfig()
{
PropertyA = SystemSettings.getSetting(KEYA);
PropertyB = SystemSettings.getSetting(KEYB);
PropertyC = SystemSettings.getSetting(KEYC);
}
}
Run Code Online (Sandbox Code Playgroud)
我想在_Layout.cshtml中初始化并输出它,以便我所有其他视图都可以使用它.到目前为止,我只能弄清楚如何在_Layout.cshtml中手动执行此操作,如下所示:
<script type="text/javascript">
MyNamespace.someConfig = {
PropertyA: @SystemSettings.getSetting(KEYA),
PropertyB: @SystemSettings.getSetting(KEYB),
PropertyC: @SystemSettings.getSetting(KEYC)
}
</script>
Run Code Online (Sandbox Code Playgroud)
这很糟糕,因为每次SomeConfig.cs模型扩展时,我都要记得在客户端手动扩展它.有没有办法在_Layout.cshtml中初始化它,以便动态创建一个Javascript对象?
我在我的Entity Framework(v5)对象上使用了一个部分类(.NET 4.5).我为此部分类添加了一个接口,但是针对此接口测试EF对象是错误的,但应该识别接口是在部分类上定义的.这是我正在尝试的:
public interface Product : ILastModified
{
public DateTime LastModified { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后在我的数据层我尝试这个:
public virtual int Update<T>(T TObject) where T : class
{
//WHY ALWAYS FALSE?
if (TObject is ILastModified)
{
(TObject as ILastModified).LastModified = DateTime.Now;
}
var entry = dbContext.Entry(TObject);
dbContext.Set<T>().Attach(TObject);
entry.State = EntityState.Modified;
return dbContext.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)
问题是"if(TObject是ILastModified)"总是假的,即使我在部分类上设置它.我做错了什么或有办法实现这样的事情吗?
我有一个基类,我希望所有派生类都在类的顶部放置一个属性,如下所示:
[MyAttribute("Abc 123")]
public class SomeClass : MyBaseClass
{
public SomeClass() : base()
{
}
}
public class MyBaseClass
{
public string PropA { get; set; }
public MyBaseClass()
{
this.PropA = //ATTRIBUTE VALUE OF DERIVED
}
}
Run Code Online (Sandbox Code Playgroud)
如何强制派生类需要该属性,然后在基构造函数中使用该属性值?
在我的中ExtensionDelegate,我想获取我的一个接口控制器的实例。我正在使用基于页面的导航。
例如,我可以这样做iOS:
if let controller = self.tabBarController?.viewControllers?[1] as? TimesController {
controller.myVariable = nil
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做watchOS?
对于NSNotificationCenter块,我必须使用[unown self]避免强引用周期:
NSNotificationCenter.defaultCenter()
.addObserverForName(UIApplicationWillEnterForegroundNotification,
object: nil,
queue: nil,
usingBlock: { [unowned self] (notification : NSNotification!) -> Void in
self.add(123)
})
Run Code Online (Sandbox Code Playgroud)
但是,在UIView.animateWithDuration中,我不必使用[unown self]:
UIView.animateWithDuration(0.5, animations: { () -> Void in
self.someOutlet.alpha = 1.0
self.someMethod()
})
Run Code Online (Sandbox Code Playgroud)
有什么不同?
我有一系列我想要执行的闭包,并以安全的方式删除每个项目.
如果我只是这样做:
array.forEach { $0() }
array.removeAll()
Run Code Online (Sandbox Code Playgroud)
项目可能在执行forEach和removeAll执行之间偷偷摸摸,所以我可能会删除在前一行中没有执行的元素.
这样的事情会更安全吗?
extension Array {
mutating func removeEach(handler: @escaping (Element) -> Void) {
enumerated().forEach { handler(remove(at: $0.offset)) }
}
}
Run Code Online (Sandbox Code Playgroud)
有没有一种安全的方法以算法的方式执行此操作而不是使用线程锁?
我有一堆带有结果完成处理程序的函数,我喜欢将它们转换为 RxSwift。
\n\n他们遵循这个约定:
\n\nfunc fetch(id: Int, completion: @escaping (Result<AuthorType, DataError>) -> Void) {...}\nRun Code Online (Sandbox Code Playgroud)\n\n我可以使用典型的:
\n\nreturn Observable<AuthorType>.create { on(.next... }\nRun Code Online (Sandbox Code Playgroud)\n\n有没有像 PromiseKit 那样更周到的通用方法:
\n\nfunc fetch() -> Promise<AuthorType> {\nreturn Promise { fetch(completion: $0.resolve) }\nRun Code Online (Sandbox Code Playgroud)\n\n}
\n\nRxSwift 中可能有类似的事情吗?
\n在我的 iOS 14 中App,我可以AppDelegate通过执行以下操作来注册旧版:
@main
struct MyApp: App {
#if os(iOS)
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
#endif
var body: some Scene {
...
}
}
#if os(iOS)
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
SomeSDK.configure(with: launchOptions)
return true
}
}
#endif
Run Code Online (Sandbox Code Playgroud)
但是,我在文档中注意到您可以制作UIApplicationDelegateAdaptor一个ObservableObject然后它将它注入到EnvironmentObject:
...delegate 将被放置在 Environment 中,并且可以通过使用
@EnvironmentObject视图层次结构中的属性包装器来访问。
我找不到任何有关如何工作的示例。使这项工作作为一个的语法是ObservableObject什么?
我想提供Combine完成关闭的对应项,这变得非常麻烦。是否有更短的方法或扩展可以转换以下内容:
extension MyType {
func send(with request: URLRequest, completion: @escaping (Result<MyResponse, MyError>) -> Void) {
// Logic
}
func send(with request: URLRequest) -> Future<MyResponse, MyError> {
Future<MyResponse, MyError> { promise in
send(with: request) { result in
switch result {
case let .success(response):
promise(.success(response))
case let .failure(error):
promise(.failure(error))
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
该Future方法只是完成闭包方法的包装器。我希望至少能做这样的事情:
Future<MyResponse, MyError> { send(with:request, completion: $0) }
Run Code Online (Sandbox Code Playgroud)
有没有更优雅的方法来做到这一点,因为这将应用于我图书馆的很多地方。
我试图仅在以模态方式呈现时才在视图上有条件地显示自定义按钮。这可以在 UIKit 中完成,但我正在尝试在 SwiftUI 中完成此操作。
我尝试使用环境变量presentationMode来检查它是否正在显示,但在这两种情况下标志都是 true:
@Environment(\.presentationMode) private var presentationMode
if presentationMode.wrappedValue.isPresented { // true in both cases
...
}
Run Code Online (Sandbox Code Playgroud)
有没有办法让视图知道它是否正在呈现或是否已推送?
额外上下文:
我正在尝试创建一个自定义修改器,它将自动在这两种情况下重用的视图上添加一个关闭按钮:
struct OverlayDismissButtonModifier: ViewModifier {
@Environment(\.presentationMode) private var presentationMode
func body(content: Content) -> some View {
content
.overlay(
Group {
if presentationMode.wrappedValue.isPresented { // <-- True in both cases :(
Button(action: { presentationMode.wrappedValue.dismiss() }) {
Label(LocalizedStringKey("Close"), systemImage: "xmark")
.labelStyle(IconOnlyLabelStyle())
.foregroundColor(Color(.label))
.padding(8)
.background(
Circle()
.fill(Color(.systemBackground).opacity(0.8))
)
}
.padding([.top, .trailing])
}
},
alignment: .topTrailing
) …Run Code Online (Sandbox Code Playgroud) swift ×6
c# ×3
asp.net ×2
ios ×2
swiftui ×2
.net ×1
algorithm ×1
appdelegate ×1
arrays ×1
asp.net-mvc ×1
asynchronous ×1
client-side ×1
combine ×1
ios14 ×1
javascript ×1
oop ×1
rx-swift ×1
swift2 ×1
swift4 ×1
watchos ×1
watchos-2 ×1