想了解在Window上设置设计时DataContext会导致编译器错误?的新动态吗?本文将为您提供详细的信息,此外,我们还将为您介绍关于.net–在WPF中,如何从包含ListBox的DataTemp
想了解在 Window 上设置设计时 DataContext 会导致编译器错误?的新动态吗?本文将为您提供详细的信息,此外,我们还将为您介绍关于.net – 在WPF中,如何从包含ListBox的DataTemplate内部数据绑定到Window DataContext?、android-Room Dao LiveData作为返回类型导致编译时错误、asp.net – 将linq连接到sql datacontext到业务层中的httpcontext、c – `if constexpr`,内部lambda,内部包扩展 – 编译器错误?的新知识。
本文目录一览:- 在 Window 上设置设计时 DataContext 会导致编译器错误?
- .net – 在WPF中,如何从包含ListBox的DataTemplate内部数据绑定到Window DataContext?
- android-Room Dao LiveData作为返回类型导致编译时错误
- asp.net – 将linq连接到sql datacontext到业务层中的httpcontext
- c – `if constexpr`,内部lambda,内部包扩展 – 编译器错误?
在 Window 上设置设计时 DataContext 会导致编译器错误?
我的 WPF 应用程序的主窗口在下面有以下 XAML,我正在尝试设置d:DataContext
下面的设计时间,我可以成功地为我的所有各种
UserControls 执行此操作,但是当我尝试在窗户…
Error 1 The property ''DataContext'' must be in the default namespace or in theelement namespace ''http://schemas.microsoft.com/winfx/2006/xaml/presentation''.Line 8 Position 9. C:\dev\bplus\PMT\src\UI\MainWindow.xaml 8 9 UI
<Window x:xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:UI="clr-namespace:BenchmarkPlus.PMT.UI" xmlns:Controls="clr-namespace:BenchmarkPlus.PMT.UI.Controls" d:DataContext="{d:DesignInstance Type=UI:MainViewModel, IsDesignTimeCreatable=True}" Title="MainWindow" Height="1000" Width="1600" Background="#FF7A7C82"> <Grid> <!-- Content Here --> </grid></Window>
答案1
小编典典我需要将mc:Ignorable="d"
属性添加到 Window 标签。基本上我学到了一些新东西。d:
Expression Blend/Visual
Studio 设计器承认的命名空间前缀实际上被真正的编译器/xaml 解析器 忽略/“注释掉” !
<Window ... xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008".../>
以下内容取自
内森,亚当 (2010-06-04)。 WPF 4 发布(Kindle 位置
1799-1811)。萨姆斯。Kindle版。
标记兼容性
标记兼容性 XML 命名空间(http://schemas.openxmlformats.org/markup-
compatibility/2006,通常与mc
前缀一起使用)包含一个 Ignorable 属性,该属性指示 XAML
处理器忽略指定命名空间中的所有元素/属性(如果它们可以)被解析为他们的 .NET 类型/成员。(命名空间还有一个 ProcessContent
属性,该属性覆盖被忽略命名空间内特定类型的 Ignorable。)
Expression Blend 利用此功能来执行一些操作,例如将设计时属性添加到 XAML 内容中,这些属性可以在运行时被忽略。
mc:Ignorable
可以给定一个以空格分隔的命名空间列表,并且可以给 mc:ProcessContent 一个以空格分隔的元素列表。当
XamlXmlReader
遇到无法解决的可忽略内容时,它不会为它报告任何节点。如果可以解决可忽略的内容,则会正常上报。因此,消费者不需要做任何特别的事情来正确处理标记兼容性。
.net – 在WPF中,如何从包含ListBox的DataTemplate内部数据绑定到Window DataContext?
查看型号:
using System.Collections.Generic; namespace Example { class Member { public string Name { get; set; } public int Age { get; set; } } class Team { private List<Member> members = new List<Member>(); public string TeamName { get; set; } public List<Member> Members { get { return members; } } } }
MainWindow.xaml:
<Window x:xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:l="clr-namespace:Example" Title="Example" Height="300" Width="300" Name="Main"> <Window.DataContext> <l:Team TeamName="The best team"> <l:Team.Members> <l:Member Name="John Doe" Age="23"/> <l:Member Name="Jane Smith" Age="20"/> <l:Member Name="Max Steel" Age="24"/> </l:Team.Members> </l:Team> </Window.DataContext> <ListBox ItemsSource="{Binding Path=Members}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Path=TeamName}" Margin="4"/> <TextBlock Text="{Binding Path=Name}" Margin="4"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Window>
当然,TeamBox的TeamName属性不会显示在ListBox项中,因为LisBox的每个项都是List.ItemTemplate的DataContext,它会覆盖Window的DataContext.
问题是:如何从ListBox的DataTemplate中数据绑定到视图模型(Window.DataContext)的TeamName属性?
<Window x:xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:l="clr-namespace:Example" Title="Example" Height="300" Width="300" Name="Main"> <Window.Resources> <l:Team x:Key="data" TeamName="The best team"> <l:Team.Members> <l:Member Name="John Doe" Age="23"/> <l:Member Name="Jane Smith" Age="20"/> <l:Member Name="Max Steel" Age="24"/> </l:Team.Members> </l:Team> <Window.Resources> <Window.DataContext> <StaticResource ResourceKey="data"/> </Window.DataContext> <ListBox ItemsSource="{Binding Path=Members}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Source={StaticResource data},Path=TeamName}" Margin="4"/> <TextBlock Text="{Binding Path=Name}" Margin="4"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Window>
android-Room Dao LiveData作为返回类型导致编译时错误
我正在使用Room并实现了返回LiveData的Dao.添加下面的依赖关系,它工作正常.
implementation "androidx.room:room-runtime:2.1.0-alpha04"
kapt "androidx.room:room-compiler:2.1.0-alpha04"
但是,当我添加新的Room协程依赖性时,如下所述.
implementation "androidx.room:room-runtime:2.1.0-alpha04"
implementation "androidx.room:room-coroutines:2.1.0-alpha04"
kapt "androidx.room:room-compiler:2.1.0-alpha04"
下面是编译的代码
@Dao
interface AccountDao{
@Query("SELECT * FROM account_master")
suspend fun getAllAccounts(): List<Account>
}
下面是给出错误的代码.
@Dao
interface AccountDao{
@Query("SELECT * FROM account_master")
suspend fun getAllAccounts(): LiveData<List<Account>>
}
开始收到错误.
PlayGround/app/build/tmp/kapt3/stubs/debug/com/playground/www/x/datasource/dao/AccountDao.java:11: error: Not sure how to convert a Cursor to this method's return type (androidx.lifecycle.LiveData<java.util.List<com.playground.www.x.datasource.entity.Account>>).
public abstract java.lang.Object getAllAccounts(@org.jetbrains.annotations.NotNull()
有人面临类似问题吗?
解决方法:
我认为这里的解决方案实际上是不使用协程就只返回LiveData. LiveData开箱即用,返回LiveData时无需使用协程.
使用LiveData时,它已经在后台线程上对其进行了处理.当不使用LiveData时,在这种情况下,您可以使用协程(可能最终是协程通道)或RxJava2.
有关示例,请参见此代码实验室:https://codelabs.developers.google.com/codelabs/android-room-with-a-view-kotlin.在这里,它们需要用于插入的后台线程,而不需要用于返回的LiveData的后台线程.
注意:在实际的代码实验室中,DAO没有返回LiveData似乎是一个错误.我已在以下示例中对此进行了更正.
@Dao
interface WordDao {
@Query("SELECT * from word_table ORDER BY word ASC")
fun getAllWords(): LiveData<List<Word>>
@Insert
suspend fun insert(word: Word)
@Query("DELETE FROM word_table")
fun deleteall()
}
class WordRepository(private val wordDao: WordDao) {
val allWords: LiveData<List<Word>> = wordDao.getAllWords()
@WorkerThread
suspend fun insert(word: Word) {
wordDao.insert(word)
}
}
asp.net – 将linq连接到sql datacontext到业务层中的httpcontext
public class SingletonDC { public static northwindDataContext Default { get { northwindDataContext defaultInstance = (northwindDataContext)System.Web.HttpContext.Current.Items["datacontext"]; if (defaultInstance == null) { defaultInstance = new northwindDataContext(); System.Web.HttpContext.Current.Items.Add("datacontext",defaultInstance); } return defaultInstance; } } }
解决方法
有一个“结束请求”事件,您可以轻松地挂钩,例如使用您放入Global.asax.cs的代码.在Application_EndRequest()方法中,可以在需要它的列表中的每个对象上手动调用dispose().
一种方法是迭代上下文中的每个项目,测试Idisposable,然后在适当时调用dispose.
protected void Application_EndRequest(Object sender,EventArgs e) { foreach (var key in HttpContext.Current.Items.Keys) { var disposable = HttpContext.Current.Items[key] as Idisposable; if (disposable != null) { disposable.dispose(); HttpContext.Current.Items[key] = null; } } }
我认为应该这样做. ASPNET不会自动为您执行此操作.当然,在真实应用程序中使用此代码之前,您需要保护免受异常等的影响.
Vertigo的Keith Craig写了a relevant post on the topic a while ago,描述了你想要做什么作为一种模式,换句话说,一种做一些应该重复的事情.他提供了一个类来帮助解决这个问题,延迟加载DB上下文并将其放入当前上下文中.这种方法存在一些缺陷 – 您可以在该帖子的评论讨论中阅读它们.评论中还引用了一堆相关文章.
c – `if constexpr`,内部lambda,内部包扩展 – 编译器错误?
clang version 5.0.0 (trunk 305664) Target: x86_64-unkNown-linux-gnu
以下代码成功编译:
template <int... A> void f() { ([](auto) { if constexpr (A == 0) return 42; else return 3.14; }(0),...); } int main() { f<0,1>(); }
……但是这个没有:
template <int... A> void f() { ([](auto...) { // Variadic lambda if constexpr (A == 0) return 42; else return 3.14; }(),...); // No argument } int main() { f<0,1>(); }
…屈服:
<source>:7:13: error: 'auto' in return type deduced as 'double' here but deduced as 'int' in earlier return statement return 3.14; ^ <source>:3:6: note: in instantiation of function template specialization 'f()::(anonymous class)::operator()<>' requested here ([](auto...) { // Variadic lambda ^ <source>:12:5: note: in instantiation of function template specialization 'f<0,1>' requested here f<0,1>(); ^
我不希望在空参数包和伪参数之间有不同的行为.
是否存在这种差异的原因,或者这是编译器错误?
解决方法
[dcl.spec.auto]中的规则强调我的:
If the declared return type of the function contains a placeholder type,the return type of the function is deduced from non-discarded
return
statements,if any,in the body of the function ([stmt.if]).[…]
If a function with a declared return type that contains a placeholder type has multiple non-discarded
return
statements,the return type is deduced for each such return statement. If the type deduced is not the same in each deduction,the program is ill-formed.
lambda中的一个或另一个return语句被丢弃(如果constexpr被称为废弃语句,则取非分支),只留下一个非废弃的return语句,因此lambda的返回类型应该简单地从中推断出来.一个遗留下来.
此外,clang还可以这样:
template <int A> void f() { [](auto...) { if constexpr (A == 0) return 42; else return 3.14; }(); } int main() { f<0>(); f<1>(); }
所以这可能与lambdas在pack表达式中的工作方式有一些不良的交互.
今天关于在 Window 上设置设计时 DataContext 会导致编译器错误?的介绍到此结束,谢谢您的阅读,有关.net – 在WPF中,如何从包含ListBox的DataTemplate内部数据绑定到Window DataContext?、android-Room Dao LiveData作为返回类型导致编译时错误、asp.net – 将linq连接到sql datacontext到业务层中的httpcontext、c – `if constexpr`,内部lambda,内部包扩展 – 编译器错误?等更多相关知识的信息可以在本站进行查询。
本文标签: